Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler Refactor #680

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
/lib/
/bin/
/.shards/
.vscode
.vscode
node_modules
coverage
4 changes: 3 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
crystal 1.9.1
crystal 1.10.1
mint 0.19.0
nodejs 20.10.0
yarn 1.22.19
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ local: build
documentation:
rm -rf docs && crystal docs

# This builds the binary and depends on files in "src" and "core" directories.
bin/mint: $(shell find src -type f) $(shell find core/source -type f)
src/assets/runtime.js: $(shell find runtime/src -type f)
cd runtime && make index

src/assets/runtime_test.js: $(shell find runtime/src -type f)
cd runtime && make index_testing

# This builds the binary and depends on files in some directories.
bin/mint: \
$(shell find core/source -type f) \
$(shell find runtime/src -type f) \
$(shell find src -type f) \
src/assets/runtime_test.js \
src/assets/runtime.js
shards build --error-on-warnings --error-trace --progress
13 changes: 5 additions & 8 deletions core/source/Array.mint
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ module Array {
Array.any([1, 3], (number : Number) : Bool { number % 2 == 0 }) == false
*/
fun any (array : Array(item), function : Function(item, Bool)) : Bool {
case Array.find(array, function) {
Maybe.Nothing => false
Maybe.Just => true
}
Array.find(array, function) != Maybe.Nothing
}

/*
Expand All @@ -20,7 +17,7 @@ module Array {
Array.append([1, 1, 2] [3, 5, 8]) == [1, 1, 2, 3, 5, 8]
*/
fun append (array1 : Array(item), array2 : Array(item)) : Array(item) {
`[].concat(#{array1}).concat(#{array2})`
`[...#{array1}, ...#{array2}]`
}

/*
Expand Down Expand Up @@ -70,7 +67,7 @@ module Array {
`
(() => {
for (let item of #{array}) {
if (_compare(#{other}, item)) {
if (#{%compare%}(#{other}, item)) {
return true
}
}
Expand Down Expand Up @@ -98,7 +95,7 @@ module Array {
`
(() => {
if (#{index} < 0 || #{index} >= #{array}.length) { return #{array} }
const result = Array.from(#{array})
const result = [...#{array}]
result.splice(#{index}, 1)
return result
})()
Expand Down Expand Up @@ -273,7 +270,7 @@ module Array {
`
(() => {
for (let index = 0; index < #{array}.length; index++) {
if (_compare(#{value}, #{method}(#{array}[index]))) {
if (#{%compare%}(#{value}, #{method}(#{array}[index]))) {
return index
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/source/Html.Portals.Body.mint
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ component Html.Portals.Body {

/* Renders the children into the document's body. */
fun render : Html {
`_createPortal(#{children}, document.body)`
`#{%createPortal%}(#{children}, document.body)`
}
}
2 changes: 1 addition & 1 deletion core/source/Html.Portals.Head.mint
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ component Html.Portals.Head {

/* Renders the children into the document's head. */
fun render : Html {
`_createPortal(#{children}, document.head)`
`#{%createPortal%}(#{children}, document.head)`
}
}
27 changes: 11 additions & 16 deletions core/source/Http.mint
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,18 @@ case (request) {
```
*/
module Http {
const REQUESTS = `{}`

/*
Aborts all running requests.

Http.abortAll()
*/
fun abortAll : Void {
`
this._requests && Object.keys(this._requests).forEach((uid) => {
this._requests[uid].abort()
delete this._requests[uid]
#{REQUESTS} && Object.keys(#{REQUESTS}).forEach((uid) => {
#{REQUESTS}[uid].abort()
delete #{REQUESTS}[uid]
})
`
}
Expand Down Expand Up @@ -245,11 +247,6 @@ module Http {
|> url(urlValue)
}

/* Returns all running requests. */
fun requests : Map(String, Http.NativeRequest) {
`this._requests`
}

/*
Sends the request with a unique ID (generated by default) so it could be aborted later.

Expand All @@ -263,11 +260,9 @@ module Http {
) : Promise(Result(Http.ErrorResponse, Http.Response)) {
`
new Promise((resolve, reject) => {
if (!this._requests) { this._requests = {} }

let xhr = new XMLHttpRequest()

this._requests[#{uid}] = xhr
#{REQUESTS}[#{uid}] = xhr

xhr.withCredentials = #{request.withCredentials}
xhr.responseType = "blob"
Expand All @@ -286,7 +281,7 @@ module Http {
try {
xhr.open(#{request.method}.toUpperCase(), #{request.url}, true)
} catch (error) {
delete this._requests[#{uid}]
delete #{REQUESTS}[#{uid}]

resolve(#{Result::Err({
headers: `getResponseHeaders()`,
Expand All @@ -301,7 +296,7 @@ module Http {
})

xhr.addEventListener('error', (event) => {
delete this._requests[#{uid}]
delete #{REQUESTS}[#{uid}]

resolve(#{Result::Err({
headers: `getResponseHeaders()`,
Expand All @@ -312,7 +307,7 @@ module Http {
})

xhr.addEventListener('timeout', (event) => {
delete this._requests[#{uid}]
delete #{REQUESTS}[#{uid}]

resolve(#{Result::Err({
headers: `getResponseHeaders()`,
Expand All @@ -323,7 +318,7 @@ module Http {
})

xhr.addEventListener('load', async (event) => {
delete this._requests[#{uid}]
delete #{REQUESTS}[#{uid}]

let contentType = xhr.getResponseHeader("Content-Type");
let responseText = await xhr.response.text();
Expand Down Expand Up @@ -377,7 +372,7 @@ module Http {
})

xhr.addEventListener('abort', (event) => {
delete this._requests[#{uid}]
delete #{REQUESTS}[#{uid}]

resolve(#{Result::Err({
headers: `getResponseHeaders()`,
Expand Down
6 changes: 3 additions & 3 deletions core/source/Locale.mint
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module Locale {
/* Sets the current locale. */
fun set (locale : String) : Bool {
`_L.set(#{locale})`
`#{%setLocale%}(#{locale})`
}

/* Returns the current locale. */
fun get : Maybe(String) {
`
(() => {
if (_L.locale) {
return #{Maybe::Just(`_L.locale`)}
if (#{%locale%}) {
return #{Maybe::Just(`#{%locale%}`)}
} else {
return #{Maybe::Nothing}
}
Expand Down
2 changes: 1 addition & 1 deletion core/source/Map.mint
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ module Map {
let set = false

for (let item of #{map}) {
if (_compare(item[0], #{key})) {
if (#{%compare%}(item[0], #{key})) {
set = true
result.push([#{key}, #{value}])
} else {
Expand Down
14 changes: 7 additions & 7 deletions core/source/Object/Decode.mint
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Object.Decode {
input : Object,
decoder : Function(Object, Result(Object.Error, a))
) : Result(Object.Error, Array(a)) {
`Decoder.array(#{decoder})(#{input})`
`#{%decodeArray%}(#{decoder}, #{%ok%}, #{%err%})(#{input})`
}

/*
Expand All @@ -18,7 +18,7 @@ module Object.Decode {
Object.Decode.boolean(`true`) == Result::Ok(true)
*/
fun boolean (input : Object) : Result(Object.Error, Bool) {
`Decoder.boolean(#{input})`
`#{%decodeBoolean%}(#{%ok%}, #{%err%})(#{input})`
}

/*
Expand All @@ -32,7 +32,7 @@ module Object.Decode {
key : String,
decoder : Function(Object, Result(Object.Error, a))
) : Result(Object.Error, a) {
`Decoder.field(#{key}, #{decoder})(#{input})`
`#{%decodeField%}(#{key}, #{decoder}, #{%err%})(#{input})`
}

/*
Expand All @@ -45,7 +45,7 @@ module Object.Decode {
input : Object,
decoder : Function(Object, Result(Object.Error, a))
) : Result(Object.Error, Maybe(a)) {
`Decoder.maybe(#{decoder})(#{input})`
`#{%decodeMaybe%}(#{decoder}, #{%ok%}, #{%err%}, #{%just%}, #{%nothing%})(#{input})`
}

/*
Expand All @@ -54,7 +54,7 @@ module Object.Decode {
Object.Decode.number(`0`) == Result::Ok(0)
*/
fun number (input : Object) : Result(Object.Error, Number) {
`Decoder.number(#{input})`
`#{%decodeNumber%}(#{%ok%}, #{%err%})(#{input})`
}

/*
Expand All @@ -63,7 +63,7 @@ module Object.Decode {
Object.Decode.string(`"A"`) == Result::Ok("A")
*/
fun string (input : Object) : Result(Object.Error, String) {
`Decoder.string(#{input})`
`#{%decodeString%}(#{%ok%}, #{%err%})(#{input})`
}

/*
Expand All @@ -72,6 +72,6 @@ module Object.Decode {
Object.Decode.time(`"new Date()"`)
*/
fun time (input : Object) : Result(Object.Error, Time) {
`Decoder.time(#{input})`
`#{%decodeTime%}(#{%ok%}, #{%err%})(#{input})`
}
}
4 changes: 2 additions & 2 deletions core/source/Set.mint
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Set {
const newSet = []

#{set}.forEach((item) => {
if (_compare(item, #{value})) { return }
if (#{%compare%}(item, #{value})) { return }
newSet.push(item)
})

Expand Down Expand Up @@ -67,7 +67,7 @@ module Set {
`
(() => {
for (let item of #{set}) {
if (_compare(item, #{value})) {
if (#{%compare%}(item, #{value})) {
return true
}
}
Expand Down
29 changes: 26 additions & 3 deletions core/source/Test/Context.mint
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Test.Context {
fun assertEqual (context : Test.Context(a), value : a) : Test.Context(a) {
`
#{context}.step((subject) => {
if (!_compare(#{value}, subject)) {
if (!#{%compare%}(#{value}, subject)) {
throw \`Assertion failed: ${#{value}} === ${subject}\`
}
return subject
Expand Down Expand Up @@ -61,14 +61,37 @@ module Test.Context {
#{context}.step((subject) => {
const actual = #{method}(subject)

if (!_compare(#{value}, actual)) {
if (!#{%compare%}(#{value}, actual)) {
throw \`Assertion failed: ${actual} === ${#{value}}\`
}
return subject
})
`
}

/*
Asserts if the given value equals of the returned value from the given
function.

test {
Test.Context.of(5)
|> Test.Context.assertOf("5", Number.toString)
}
*/
fun assert (
context : Test.Context(a),
method : Function(a, bool)
) : Test.Context(a) {
`
#{context}.step((subject) => {
if (!#{method}(subject)) {
throw \`Assertion failed!\`
}
return subject
})
`
}

/*
Maps the given subject to a new subject.

Expand All @@ -90,7 +113,7 @@ module Test.Context {
}
*/
fun of (a : a) : Test.Context(a) {
`new TestContext(#{a})`
`new #{%testContext%}(#{a})`
}

/* Spies on the given entity if it's a function. */
Expand Down
10 changes: 5 additions & 5 deletions core/source/Test/Html.mint
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ module Test.Html {
Test.Html.start(<div><{ "Content" }></div>)
*/
fun start (node : Html) : Test.Context(Dom.Element) {
`
(`
(() => {
const root = document.createElement("div")
document.body.appendChild(root)
ReactDOM.render(#{node}, root)
return new TestContext(root, () => {
ReactDOM.unmountComponentAtNode(root)
#{%testRender%}(#{node}, root)
return new #{%testContext%}(root, () => {
document.body.removeChild(root)
})
})()
`
` as Test.Context(Dom.Element))
|> Test.Context.timeout(0)
}

fun find (
Expand Down
Loading