From ed317e62eb1cf98fde4461fc90c6cb1045ebc7e8 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sat, 25 Jan 2020 17:48:03 +0800 Subject: fix Moonscript issue 375. --- README.md | 66 ++++++++++++++++-- spec/inputs/assign.moon | 24 +++---- spec/inputs/bubbling.moon | 8 +-- spec/inputs/class.moon | 158 +++++++++++++++++++++---------------------- spec/inputs/cond.moon | 104 ++++++++++++++-------------- spec/inputs/destructure.moon | 112 +++++++++++++++--------------- spec/inputs/do.moon | 26 +++---- spec/inputs/existential.moon | 22 +++++- spec/inputs/export.moon | 90 ++++++++++++------------ spec/inputs/funcs.moon | 104 ++++++++++++++-------------- spec/inputs/import.moon | 54 +++++++-------- spec/inputs/lists.moon | 14 ++-- spec/inputs/local.moon | 108 ++++++++++++++--------------- spec/inputs/loops.moon | 102 ++++++++++++++-------------- spec/inputs/operators.moon | 54 +++++++-------- spec/inputs/plus.moon | 6 ++ spec/inputs/return.moon | 66 +++++++++--------- spec/inputs/stub.moon | 6 +- spec/inputs/switch.moon | 72 ++++++++++---------- spec/inputs/syntax.moon | 78 ++++++++++----------- spec/inputs/tables.moon | 138 ++++++++++++++++++------------------- spec/inputs/unless_else.moon | 8 +-- spec/inputs/using.moon | 16 ++--- spec/inputs/whitespace.moon | 70 +++++++++---------- spec/inputs/with.moon | 136 ++++++++++++++++++------------------- src/MoonP/moon_ast.h | 3 +- src/MoonP/moon_compiler.cpp | 1 + src/MoonP/moon_parser.cpp | 4 +- 28 files changed, 867 insertions(+), 783 deletions(-) diff --git a/README.md b/README.md index 2385a11..7421829 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # MoonPlus ![CI](https://github.com/pigpigyyy/MoonPlus/workflows/build-test/badge.svg) -MoonPlus is a compiler adopting features from Moonscript language 0.5.0 and could be 2~4 times faster than the original Moonscript compiler. +MoonPlus is a compiler with features from Moonscript language 0.5.0 and could be 2~4 times faster than the original Moonscript compiler. ## Features @@ -10,19 +10,75 @@ MoonPlus is a compiler adopting features from Moonscript language 0.5.0 and coul * Support full Moonscript language features, generate the same Lua codes with original compiler. * Reserve line numbers from source Moonscript codes in compiled Lua codes to help with debugging. -## Minor Changes +## Changes -* Can do slash call with Lua keyword. Generate codes from: +* Add existential operator support. Generate codes from: ```Moonscript -c.repeat.if\then("xyz")\else res +func?! + +x = tab?.value + +print abc?["hello world"]?.xyz + +if print and x? + print x ```   to: +```Lua +if func ~= nil then + func() +end +local x +if tab ~= nil then + x = tab.value +end +print((function() + if abc ~= nil then + local _obj_0 = abc["hello world"] + if _obj_0 ~= nil then + return _obj_0.xyz + end + return nil + end + return nil +end)()) +if print and (x ~= nil) then + print(x) +end +``` + +* Can do slash call with Lua keywords. Generate codes from: ```Moonscript +c.repeat.if\then("xyz")\else res +``` +  to: +```Lua local _call_3 = c["repeat"]["if"] local _call_4 = _call_3["then"](_call_3, "xyz") _call_4["else"](_call_4, res) ``` +* Add more usage for `import` keyword. Will compile codes from: +```Moonscript +import 'module' +import "module.part" +import "d-a-s-h-e-s" +import "player" as Player +import "lpeg" as {:C, :Ct, :Cmt} +``` +  to: +```Lua +local module = require('module') +local part = require("module.part") +local d_a_s_h_e_s = require("d-a-s-h-e-s") +local Player = require("player") +local C, Ct, Cmt +do + local _obj_0 = require("lpeg") + C, Ct, Cmt = _obj_0.C, _obj_0.Ct, _obj_0.Cmt +end +``` + * Add feature of `reusing variable` which helps generate reduced Lua codes. For example, MoonPlus will generate codes from: ```Moonscript with leaf @@ -36,7 +92,7 @@ for x in *something print x ```   to: -```lua +```Lua leaf.world(1, 2, 3) do local g = leaf.what.is.this diff --git a/spec/inputs/assign.moon b/spec/inputs/assign.moon index 3e66491..4e50147 100644 --- a/spec/inputs/assign.moon +++ b/spec/inputs/assign.moon @@ -1,30 +1,30 @@ _ = -> - joop = 2302 + joop = 2302 - (hi) -> - d = 100 - hi = 1021 + (hi) -> + d = 100 + hi = 1021 - a,b,c,d = 1,2,3,4 + a,b,c,d = 1,2,3,4 - hello[232], (5+5)[121], hello, x[99] = 100, 200, 300 + hello[232], (5+5)[121], hello, x[99] = 100, 200, 300 - joop = 12 + joop = 12 joop = 2345 a, b = if hello - "hello" + "hello" else - "nothing", "yeah" + "nothing", "yeah" a, b = if hello - if yeah then "one", "two" else "mmhh" + if yeah then "one", "two" else "mmhh" else - print "the other" - "nothing", "yeah" + print "the other" + "nothing", "yeah" diff --git a/spec/inputs/bubbling.moon b/spec/inputs/bubbling.moon index d1004f9..62d1550 100644 --- a/spec/inputs/bubbling.moon +++ b/spec/inputs/bubbling.moon @@ -3,17 +3,17 @@ f = (...) -> #{...} dont_bubble = -> - [x for x in ((...)-> print ...)("hello")] + [x for x in ((...)-> print ...)("hello")] k = [x for x in ((...)-> print ...)("hello")] j = for i=1,10 - (...) -> print ... + (...) -> print ... -- bubble me m = (...) -> - [x for x in *{...} when f(...) > 4] + [x for x in *{...} when f(...) > 4] x = for i in *{...} do i y = [x for x in *{...}] @@ -23,6 +23,6 @@ z = [x for x in hallo when f(...) > 4] a = for i=1,10 do ... b = for i=1,10 - -> print ... + -> print ... diff --git a/spec/inputs/class.moon b/spec/inputs/class.moon index 60361ca..83f3760 100644 --- a/spec/inputs/class.moon +++ b/spec/inputs/class.moon @@ -1,10 +1,10 @@ class Hello - new: (@test, @world) => - print "creating object.." - hello: => - print @test, @world - __tostring: => "hello world" + new: (@test, @world) => + print "creating object.." + hello: => + print @test, @world + __tostring: => "hello world" x = Hello 1,2 x\hello() @@ -12,26 +12,26 @@ x\hello() print x class Simple - cool: => print "cool" + cool: => print "cool" class Yikes extends Simple - new: => print "created hello" + new: => print "created hello" x = Yikes() x\cool() class Hi - new: (arg) => - print "init arg", arg + new: (arg) => + print "init arg", arg - cool: (num) => - print "num", num + cool: (num) => + print "num", num class Simple extends Hi - new: => super "man" - cool: => super 120302 + new: => super "man" + cool: => super 120302 x = Simple() x\cool() @@ -40,45 +40,45 @@ print x.__class == Simple class Okay - -- what is going on - something: 20323 - -- yeaha + -- what is going on + something: 20323 + -- yeaha class Biggie extends Okay - something: => - super 1,2,3,4 - super.something another_self, 1,2,3,4 - assert super == Okay + something: => + super 1,2,3,4 + super.something another_self, 1,2,3,4 + assert super == Okay class Yeah - okay: => - super\something 1,2,3,4 + okay: => + super\something 1,2,3,4 class What - something: => print "val:", @val + something: => print "val:", @val class Hello extends What - val: 2323 - something: => super\something + val: 2323 + something: => super\something with Hello! - x = \something! - print x - x! + x = \something! + print x + x! class CoolSuper - hi: => - super(1,2,3,4) 1,2,3,4 - super.something 1,2,3,4 - _ = super.something(1,2,3,4).world - super\yeah"world".okay hi, hi, hi - _ = something.super - _ = super.super.super.super - _ = super\hello - nil + hi: => + super(1,2,3,4) 1,2,3,4 + super.something 1,2,3,4 + _ = super.something(1,2,3,4).world + super\yeah"world".okay hi, hi, hi + _ = something.super + _ = super.super.super.super + _ = super\hello + nil -- selfing @@ -95,11 +95,11 @@ xx = (@hello, @@world, cool) -> -- class properties class ClassMan - @yeah: 343 - blue: => - @hello: 3434, @world: 23423 - green: => - @red: => + @yeah: 343 + blue: => + @hello: 3434, @world: 23423 + green: => + @red: => x = @ @@ -118,34 +118,34 @@ _ = hello[@].world class Whacko - _ = @hello - if something - print "hello world" + _ = @hello + if something + print "hello world" - hello = "world" - @another = "day" + hello = "world" + @another = "day" - print "yeah" if something -- this is briken + print "yeah" if something -- this is briken print "hello" yyy = -> - class Cool - _ = nil + class Cool + _ = nil -- class a.b.c.D - _ = nil + _ = nil class a.b["hello"] - _ = nil + _ = nil class (-> require "moon")!.Something extends Hello.World - _ = nil + _ = nil -- @@ -160,54 +160,54 @@ print (class WhatsUp).__name export ^ class Something - _ = nil + _ = nil -- -- hoisting class Something - val = 23 - {:insert} = table - new: => print insert, val -- prints nil 23 + val = 23 + {:insert} = table + new: => print insert, val -- prints nil 23 -- class X - new: hi + new: hi -- class Cool extends Thing - dang: => - { - hello: -> super! - world: -> super.one - } + dang: => + { + hello: -> super! + world: -> super.one + } -- class Whack extends Thing - dang: do_something => - super! + dang: do_something => + super! --- class Wowha extends Thing - @butt: -> - super! - _ = super.hello - super\hello! - super\hello - - - @zone: cool { - -> - super! - _ = super.hello - super\hello! - super\hello - } + @butt: -> + super! + _ = super.hello + super\hello! + super\hello + + + @zone: cool { + -> + super! + _ = super.hello + super\hello! + super\hello + } nil diff --git a/spec/inputs/cond.moon b/spec/inputs/cond.moon index e8b6283..3dee99b 100644 --- a/spec/inputs/cond.moon +++ b/spec/inputs/cond.moon @@ -2,24 +2,24 @@ you_cool = false _ = if cool - if you_cool - one - else if eatdic - yeah - else - _ = two - three + if you_cool + one + else if eatdic + yeah + else + _ = two + three else - no + no _ = if cool then no _ = if cool then no else yes if cool then wow cool else - noso cool + noso cool if working - _ = if cool then if cool then okay else what else nah + _ = if cool then if cool then okay else what else nah if yeah then no day elseif cool me then okay ya else u way @@ -29,117 +29,117 @@ if yeah then no dad else if cool you then okay bah else p way if (->)() then what ever if nil then flip me else - it be,rad + it be,rad if things great then no way elseif okay sure - what here + what here if things then no chance elseif okay - what now + what now if things - yes man + yes man elseif okay person then hi there else hmm sure if lets go - print "greetings" + print "greetings" elseif "just us" - print "will smith" else show 5555555 + print "will smith" else show 5555555 -- if something = 10 - print something + print something else - print "else" + print "else" hello = if something = 10 - print something + print something else - print "else" + print "else" hello = 5 + if something = 10 - print something + print something --- z = false _ = if false - one + one elseif x = true - two + two elseif z = true - three + three else - four + four out = if false - one + one elseif x = true - two + two elseif z = true - three + three else - four + four kzy = -> - if something = true - 1 - elseif another = false - 2 + if something = true + 1 + elseif another = false + 2 --- unless true - print "cool!" + print "cool!" unless true and false - print "cool!" + print "cool!" unless false then print "cool!" unless false then print "cool!" else print "no way!" unless nil - print "hello" + print "hello" else - print "world" + print "world" -- x = unless true - print "cool!" + print "cool!" x = unless true and false - print "cool!" + print "cool!" y = unless false then print "cool!" y = unless false then print "cool!" else print "no way!" z = unless nil - print "hello" + print "hello" else - print "world" + print "world" print unless true - print "cool!" + print "cool!" print unless true and false - print "cool!" + print "cool!" print unless false then print "cool!" print unless false then print "cool!" else print "no way!" print unless nil - print "hello" + print "hello" else - print "world" + print "world" -- @@ -151,9 +151,9 @@ dddd = {1,2,3} unless value -- do - j = 100 - unless j = hi! - error "not j!" + j = 100 + unless j = hi! + error "not j!" ---------------- @@ -165,8 +165,8 @@ a,c,b = "cool" if something --- j = if 1 - if 2 - 3 + if 2 + 3 else 6 @@ -174,10 +174,10 @@ m = if 1 - if 2 + if 2 - 3 + 3 else 6 diff --git a/spec/inputs/destructure.moon b/spec/inputs/destructure.moon index 5bc7810..e0f2198 100644 --- a/spec/inputs/destructure.moon +++ b/spec/inputs/destructure.moon @@ -1,105 +1,105 @@ do - {a, b} = hello + {a, b} = hello - {{a}, b, {c}} = hello + {{a}, b, {c}} = hello - { :hello, :world } = value + { :hello, :world } = value do - { yes: no, thing } = world + { yes: no, thing } = world - {:a,:b,:c,:d} = yeah + {:a,:b,:c,:d} = yeah - {a} = one, two - {b}, c = one - {d}, e = one, two + {a} = one, two + {b}, c = one + {d}, e = one, two - x, {y} = one, two + x, {y} = one, two - xx, yy = 1, 2 - {yy, xx} = {xx, yy} + xx, yy = 1, 2 + {yy, xx} = {xx, yy} - {a, :b, c, :d, e, :f, g} = tbl + {a, :b, c, :d, e, :f, g} = tbl --- do - futurists = - sculptor: "Umberto Boccioni" - painter: "Vladimir Burliuk" - poet: - name: "F.T. Marinetti" - address: { - "Via Roma 42R" - "Bellagio, Italy 22021" - } - - {poet: {:name, address: {street, city}}} = futurists + futurists = + sculptor: "Umberto Boccioni" + painter: "Vladimir Burliuk" + poet: + name: "F.T. Marinetti" + address: { + "Via Roma 42R" + "Bellagio, Italy 22021" + } + + {poet: {:name, address: {street, city}}} = futurists -- do - { @world } = x - { a.b, c.y, func!.z } = x + { @world } = x + { a.b, c.y, func!.z } = x - { world: @world } = x + { world: @world } = x -- do - thing = {{1,2}, {3,4}} + thing = {{1,2}, {3,4}} - for {x,y} in *thing - print x,y + for {x,y} in *thing + print x,y -- do - with {a,b} = thing - print a, b + with {a,b} = thing + print a, b -- do - thing = nil - if {a} = thing - print a - else - print "nothing" - - thang = {1,2} - if {a,b} = thang - print a,b - - if {a,b} = thing - print a,b - elseif {c,d} = thang - print c,d - else - print "NO" + thing = nil + if {a} = thing + print a + else + print "nothing" + + thang = {1,2} + if {a,b} = thang + print a,b + + if {a,b} = thing + print a,b + elseif {c,d} = thang + print c,d + else + print "NO" -- do - z = "yeah" - {a,b,c} = z + z = "yeah" + {a,b,c} = z do - {a,b,c} = z + {a,b,c} = z _ = (z) -> - {a,b,c} = z + {a,b,c} = z do - z = "oo" - _ = (k) -> - {a,b,c} = z + z = "oo" + _ = (k) -> + {a,b,c} = z do - {function:{end:endVar}} = thing + {function:{end:endVar}} = thing do - {if:{a,b,c}} = thing + {if:{a,b,c}} = thing diff --git a/spec/inputs/do.moon b/spec/inputs/do.moon index 21e2127..2fbcbb9 100644 --- a/spec/inputs/do.moon +++ b/spec/inputs/do.moon @@ -1,27 +1,27 @@ do - print "hello" - print "world" + print "hello" + print "world" x = do - print "hello" - print "world" + print "hello" + print "world" y = do - things = "shhh" - -> "hello: " .. things + things = "shhh" + -> "hello: " .. things _ = -> if something then do "yeah" t = { - y: do - number = 100 - (x) -> x + number + y: do + number = 100 + (x) -> x + number } (y=(do - x = 10 + 2 - x), k=do - "nothing") -> do - "uhhh" + x = 10 + 2 + x), k=do + "nothing") -> do + "uhhh" diff --git a/spec/inputs/existential.moon b/spec/inputs/existential.moon index f7d5ebd..2264768 100644 --- a/spec/inputs/existential.moon +++ b/spec/inputs/existential.moon @@ -1,5 +1,7 @@ -f?! +f1?! + +f2? "arg0",123 x = tab?.value @@ -19,3 +21,21 @@ if {:x} = a?.if?\then?(123)? @?\function 998 res = b.function\do!\while?("OK")\if("def",998)\f? print res + +solipsism = true if mind? and not world? + +speed = 0 +speed or= 15 + +footprints = yeti or "bear" + +major = 'Computer Science' + +unless major? + signUpForClass 'Introduction to Wines' + +if window? + environment = 'browser (probably)' + +zip = lottery.drawWinner?!.address?.zipcode + diff --git a/spec/inputs/export.moon b/spec/inputs/export.moon index 0a56379..a8c782c 100644 --- a/spec/inputs/export.moon +++ b/spec/inputs/export.moon @@ -1,77 +1,77 @@ do - export a,b,c = 223, 343 - export cool = "dad" + export a,b,c = 223, 343 + export cool = "dad" do - export class Something - umm: "cool" + export class Something + umm: "cool" do - export a,b,c - a,b,c,d = "hello" + export a,b,c + a,b,c,d = "hello" do - What = if this - 232 - else - 4343 + What = if this + 232 + else + 4343 - export ^ + export ^ - another = 3434 - Another = 7890 + another = 3434 + Another = 7890 - if inner then Yeah = "10000" + if inner then Yeah = "10000" - What = if this - 232 - else - 4343 + What = if this + 232 + else + 4343 do - export * + export * - What = if this - 232 - else - 4343 + What = if this + 232 + else + 4343 - x,y,z = 1,2,3 + x,y,z = 1,2,3 - y = -> - hallo = 3434 + y = -> + hallo = 3434 - with tmp - j = 2000 + with tmp + j = 2000 do - export * - x = 3434 - if y then - x = 10 + export * + x = 3434 + if y then + x = 10 do - export * - if y then - x = 10 - x = 3434 + export * + if y then + x = 10 + x = 3434 do - do - export * + do + export * - k = 1212 + k = 1212 - do - h = 100 + do + h = 100 - y = -> - h = 100 - k = 100 + y = -> + h = 100 + k = 100 - h = 100 + h = 100 diff --git a/spec/inputs/funcs.moon b/spec/inputs/funcs.moon index da42db6..4176e33 100644 --- a/spec/inputs/funcs.moon +++ b/spec/inputs/funcs.moon @@ -11,9 +11,9 @@ go to the barn open -> the -> door open -> - the door - hello = -> - my func + the door + hello = -> + my func h = -> hi @@ -35,7 +35,7 @@ what! the! heck! _ = (a,b,c,d,e) -> _ = (a,a,a,a,a) -> - print a + print a _ = (x=23023) -> @@ -44,7 +44,7 @@ _ = (x=(y=()->) ->) -> _ = (x = if something then yeah else no) -> something = (hello=100, world=(x=[[yeah cool]])-> print "eat rice") -> - print hello + print hello _ = (x, y) => _ = (@x, @y) => @@ -62,99 +62,99 @@ _ = -> real_name if something -- d( - -> - print "hello world" - 10 + -> + print "hello world" + 10 ) d( - 1,2,3 - 4 - 5 - 6 + 1,2,3 + 4 + 5 + 6 - if something - print "okay" - 10 + if something + print "okay" + 10 - 10,20 + 10,20 ) f( - - )( - - )( - what - )(-> - print "srue" - 123) + + )( + + )( + what + )(-> + print "srue" + 123) -- x = (a, - b) -> - print "what" + b) -> + print "what" y = (a="hi", - b=23) -> - print "what" + b=23) -> + print "what" z = ( - a="hi", - b=23) -> - print "what" + a="hi", + b=23) -> + print "what" j = (f,g,m, - a="hi", - b=23 + a="hi", + b=23 ) -> - print "what" + print "what" y = (a="hi", - b=23, - ...) -> - print "what" + b=23, + ...) -> + print "what" y = (a="hi", - b=23, - ... + b=23, + ... ) -> - print "what" + print "what" -- args = (a - b) -> - print "what" + b) -> + print "what" args = (a="hi" - b=23) -> - print "what" + b=23) -> + print "what" args = ( - a="hi" - b=23) -> - print "what" + a="hi" + b=23) -> + print "what" args = (f,g,m - a="hi" - b=23 + a="hi" + b=23 ) -> - print "what" + print "what" @ = (n)-> - return 1 if n == 0 - n * @(n-1) + return 1 if n == 0 + n * @(n-1) nil diff --git a/spec/inputs/import.moon b/spec/inputs/import.moon index ef6f4ee..ded1ffd 100644 --- a/spec/inputs/import.moon +++ b/spec/inputs/import.moon @@ -18,50 +18,50 @@ import something from a table if indent - import okay, \well from tables[100] + import okay, \well from tables[100] do - import a, b, c from z + import a, b, c from z do - import a, - b, c from z + import a, + b, c from z do - import a - b - c from z + import a + b + c from z do - import - a - b - c from z + import + a + b + c from z do - import - a - b - c - from z + import + a + b + c + from z do - import 'module' - import 'module_x' - import "d-a-s-h-e-s" - import "module.part" + import 'module' + import 'module_x' + import "d-a-s-h-e-s" + import "module.part" do - import "player" as Player - import "lpeg" as {:C, :Ct, :Cmt} + import "player" as Player + import "lpeg" as {:C, :Ct, :Cmt} do - export * - import 'module' - import 'module_x' - import "org.package.module-y" + export * + import 'module' + import 'module_x' + import "org.package.module-y" do - import "org.package.module" as {function:func,if:ifVar} + import "org.package.module" as {function:func,if:ifVar} diff --git a/spec/inputs/lists.moon b/spec/inputs/lists.moon index ef5650c..15eb9ab 100644 --- a/spec/inputs/lists.moon +++ b/spec/inputs/lists.moon @@ -6,7 +6,7 @@ items = {1,2,3,4,5,6} _ = [z for z in ipairs items when z > 4] rad = [{a} for a in ipairs { - 1,2,3,4,5,6, + 1,2,3,4,5,6, } when good_number a] @@ -17,11 +17,11 @@ require "util" dump = (x) -> print util.dump x range = (count) -> - i = 0 - return coroutine.wrap -> - while i < count - coroutine.yield i - i = i + 1 + i = 0 + return coroutine.wrap -> + while i < count + coroutine.yield i + i = i + 1 dump [x for x in range 10] dump [{x, y} for x in range 5 when x > 2 for y in range 5] @@ -61,7 +61,7 @@ print y for y in *x[a,b,c] normal = (hello) -> - [x for x in yeah] + [x for x in yeah] test = x 1,2,3,4,5 diff --git a/spec/inputs/local.moon b/spec/inputs/local.moon index f14f575..33251a9 100644 --- a/spec/inputs/local.moon +++ b/spec/inputs/local.moon @@ -1,94 +1,94 @@ do - local a - local a,b,c + local a + local a,b,c - b,g = 23232 + b,g = 23232 do - x = 1212 - something = -> - local x - x = 1212 + x = 1212 + something = -> + local x + x = 1212 do - local * - y = 2323 - z = 2323 + local * + y = 2323 + z = 2323 do - local * - print "Nothing Here!" + local * + print "Nothing Here!" do - local ^ - x = 3434 - y = 3434 - X = 3434 - Y = "yeah" + local ^ + x = 3434 + y = 3434 + X = 3434 + Y = "yeah" do - local ^ - x,y = "a", "b" + local ^ + x,y = "a", "b" do - local * - x,y = "a", "b" + local * + x,y = "a", "b" do - local * - if something - x = 2323 + local * + if something + x = 2323 do - local * - do - x = "one" + local * + do + x = "one" - x = 100 + x = 100 - do - x = "two" + do + x = "two" do - local * - k = if what - 10 - x = 100 + local * + k = if what + 10 + x = 100 - {:a,:b, :c} = y + {:a,:b, :c} = y do - local * + local * - a = 100 - print "hi" - b = 200 + a = 100 + print "hi" + b = 200 - local * - c = 100 - print "hi" - d = 200 - d = 2323 + local * + c = 100 + print "hi" + d = 200 + d = 2323 do - local ^ - lowercase = 5 - Uppercase = 3 + local ^ + lowercase = 5 + Uppercase = 3 - class One - Five = 6 + class One + Five = 6 - class Two - class No + class Two + class No do - local * - -- this generates a nil value in the body - for a in *{} do _ = a + local * + -- this generates a nil value in the body + for a in *{} do _ = a g = 2323 -- test if anything leaked diff --git a/spec/inputs/loops.moon b/spec/inputs/loops.moon index 130570e..35427f6 100644 --- a/spec/inputs/loops.moon +++ b/spec/inputs/loops.moon @@ -1,133 +1,133 @@ for x=1,10 - print "yeah" + print "yeah" for x=1,#something - print "yeah" + print "yeah" for y=100,60,-3 - print "count down", y + print "count down", y for a=1,10 do print "okay" for a=1,10 - for b = 2,43 - print a,b + for b = 2,43 + print a,b for i in iter - for j in yeah - x = 343 + i + j - print i, j + for j in yeah + x = 343 + i + j + print i, j for x in *something - print x + print x for k,v in pairs hello do print k,v for x in y, z - print x + print x for x in y, z, k - print x + print x x = -> - for x in y - _ = y + for x in y + _ = y hello = {1,2,3,4,5} x = for y in *hello - if y % 2 == 0 - y + if y % 2 == 0 + y x = -> - for x in *hello - _ = y + for x in *hello + _ = y t = for i=10,20 do i * 2 hmm = 0 y = for j = 3,30, 8 - hmm += 1 - j * hmm + hmm += 1 + j * hmm _ = -> - for k=10,40 - _ = "okay" + for k=10,40 + _ = "okay" _ = -> - return for k=10,40 - "okay" + return for k=10,40 + "okay" while true do print "name" while 5 + 5 - print "okay world" - working man + print "okay world" + working man while also do - i work too - _ = "okay" + i work too + _ = "okay" i = 0 x = while i < 10 - i += 1 + i += 1 -- values that can'e be coerced x = for thing in *3 - y = "hello" + y = "hello" x = for x=1,2 - y = "hello" + y = "hello" -- continue while true - continue if false - print "yes" - break if true - print "no" + continue if false + print "yes" + break if true + print "no" for x=1,10 - continue if x > 3 and x < 7 - print x + continue if x > 3 and x < 7 + print x list = for x=1,10 - continue if x > 3 and x < 7 - x + continue if x > 3 and x < 7 + x for a in *{1,2,3,4,5,6} - continue if a == 1 - continue if a == 3 - print a + continue if a == 1 + continue if a == 3 + print a for x=1,10 - continue if x % 2 == 0 - for y = 2,12 - continue if y % 3 == 0 + continue if x % 2 == 0 + for y = 2,12 + continue if y % 3 == 0 while true - continue if false - break + continue if false + break while true - continue if false - return 22 + continue if false + return 22 -- do - xxx = {1,2,3,4} - for thing in *xxx - print thing + xxx = {1,2,3,4} + for thing in *xxx + print thing diff --git a/spec/inputs/operators.moon b/spec/inputs/operators.moon index 142ef62..57e58d6 100644 --- a/spec/inputs/operators.moon +++ b/spec/inputs/operators.moon @@ -3,70 +3,70 @@ x = 1 + 3 y = 1 + - 3 + 3 z = 1 + - 3 + - 4 + 3 + + 4 -- k = b and c and - g + g h = thing and - -> - print "hello world" + -> + print "hello world" -- TODO: should fail, indent still set to previous line so it thinks body is -- indented i = thing or - -> - print "hello world" + -> + print "hello world" p = thing and - -> + -> print "hello world" s = thing or - -> and 234 + -> and 234 -- u = { - color: 1 and 2 and - 3 - 4 - 4 + color: 1 and 2 and + 3 + 4 + 4 } v = { - color: 1 and - -> - "yeah" - "great" - oksy: 3 ^ + color: 1 and + -> + "yeah" + "great" + oksy: 3 ^ 2 } -- parens nno = ( - yeah + 2 ) + yeah + 2 ) nn = ( - yeah + 2 + yeah + 2 ) n = hello( - b + b ) -> hello a, - ( - yeah + - 2 - ) - - okay + ( + yeah + + 2 + ) - + okay diff --git a/spec/inputs/plus.moon b/spec/inputs/plus.moon index 7b9ef05..8a91c93 100644 --- a/spec/inputs/plus.moon +++ b/spec/inputs/plus.moon @@ -7,3 +7,9 @@ c.repeat.if\then("xyz")\else res print @for,@@function 123 +if fcolor = message\match "<%w*>" then message = message\gsub "<%->", fcolor + +message = message\gsub "<%->", fcolor if fcolor = message\match "<%w*>" + +func val if val = getvalue! + diff --git a/spec/inputs/return.moon b/spec/inputs/return.moon index 98c3104..f170ffd 100644 --- a/spec/inputs/return.moon +++ b/spec/inputs/return.moon @@ -6,48 +6,48 @@ _ = -> [x for x in *things] -- doesn't make sense on purpose do - return x for x in *things + return x for x in *things do - return [x for x in *things] + return [x for x in *things] do - return {x,y for x,y in *things} + return {x,y for x,y in *things} _ = -> - if a - if a - a - else - b - elseif b - if a - a - else - b - else - if a - a - else - b + if a + if a + a + else + b + elseif b + if a + a + else + b + else + if a + a + else + b do - return if a - if a - a - else - b - elseif b - if a - a - else - b - else - if a - a - else - b + return if a + if a + a + else + b + elseif b + if a + a + else + b + else + if a + a + else + b _ = -> a\b do a\b diff --git a/spec/inputs/stub.moon b/spec/inputs/stub.moon index f8f6c3f..b38056a 100644 --- a/spec/inputs/stub.moon +++ b/spec/inputs/stub.moon @@ -1,9 +1,9 @@ x = { - val: 100 - hello: => - print @val + val: 100 + hello: => + print @val } fn = x\val diff --git a/spec/inputs/switch.moon b/spec/inputs/switch.moon index a028f98..ac3dbea 100644 --- a/spec/inputs/switch.moon +++ b/spec/inputs/switch.moon @@ -1,64 +1,64 @@ switch value - when "cool" - print "hello world" + when "cool" + print "hello world" switch value - when "cool" - print "hello world" - else - print "okay rad" + when "cool" + print "hello world" + else + print "okay rad" switch value - when "cool" - print "hello world" - when "yeah" - _ = [[FFFF]] + [[MMMM]] - when 2323 + 32434 - print "okay" - else - print "okay rad" + when "cool" + print "hello world" + when "yeah" + _ = [[FFFF]] + [[MMMM]] + when 2323 + 32434 + print "okay" + else + print "okay rad" out = switch value - when "cool" then print "hello world" - else print "okay rad" + when "cool" then print "hello world" + else print "okay rad" out = switch value - when "cool" then xxxx - when "umm" then 34340 - else error "this failed big time" + when "cool" then xxxx + when "umm" then 34340 + else error "this failed big time" with something - switch \value! - when .okay - _ = "world" - else - _ = "yesh" + switch \value! + when .okay + _ = "world" + else + _ = "yesh" fix this call_func switch something - when 1 then "yes" - else "no" + when 1 then "yes" + else "no" -- switch hi - when hello or world - _ = greene + when hello or world + _ = greene -- switch hi - when "one", "two" - print "cool" - when "dad" - _ = no + when "one", "two" + print "cool" + when "dad" + _ = no switch hi - when 3+1, hello!, (-> 4)! - yello - else - print "cool" + when 3+1, hello!, (-> 4)! + yello + else + print "cool" diff --git a/spec/inputs/syntax.moon b/spec/inputs/syntax.moon index 351b22c..bf0cf04 100644 --- a/spec/inputs/syntax.moon +++ b/spec/inputs/syntax.moon @@ -29,7 +29,7 @@ hairy[hands][are](gross) okay okay[world] _ = (get[something] + 5)[years] -i,x = 200, 300 +i,x = 200, 300 yeah = (1 + 5) * 3 yeah = ((1+5)*3)/2 @@ -38,16 +38,16 @@ yeah = ((1+5)*3)/2 + i % 100 whoa = (1+2) * (3+4) * (4+5) _ = -> - if something - return 1,2,4 + if something + return 1,2,4 - print "hello" + print "hello" _ = -> - if hello - "heloo", "world" - else - no, way + if hello + "heloo", "world" + else + no, way _ = -> 1,2,34 @@ -82,23 +82,23 @@ _ = here(we)"go"[12123] -- this runs something = - test: 12323 - what: -> print "hello world" + test: 12323 + what: -> print "hello world" print something.test frick = hello: "world" argon = - num: 100 - world: (self) -> - print self.num - return { - something: -> print "hi from something" - } - somethin: (self, str) -> - print "string is", str - return world: (a,b) -> print "sum", a + b + num: 100 + world: (self) -> + print self.num + return { + something: -> print "hi from something" + } + somethin: (self, str) -> + print "string is", str + return world: (a,b) -> print "sum", a + b something.what() argon\world().something() @@ -127,7 +127,7 @@ print "what" if cool else whack! arg = {...} x = (...) -> - dump {...} + dump {...} x = not true @@ -167,11 +167,11 @@ _ = (if ntype(v) == "fndef" then x += 1) for v in *values hello = - something: world - if: "hello" - else: 3434 - function: "okay" - good: 230203 + something: world + if: "hello" + else: 3434 + function: "okay" + good: 230203 div class: "cool" @@ -185,29 +185,29 @@ what whack - 5 x = hello - world - something ((something = with what - \cool 100) -> - print something)! + \cool 100) -> + print something)! if something - _ = 03589 + _ = 03589 -- okay what about this else - _ = 3434 + _ = 3434 if something - _ = yeah + _ = yeah elseif "ymmm" - print "cool" + print "cool" else - _ = okay + _ = okay -- test names containing keywords @@ -220,15 +220,15 @@ z = x andb -- undelimited tables while 10 > something - something: "world" - print "yeah" + something: "world" + print "yeah" x = - okay: sure + okay: sure yeah - okay: man - sure: sir + okay: man + sure: sir hello "no comma" yeah: dada @@ -240,8 +240,8 @@ hello "comma", -- creates two tables another hello, one, - two, three, four, yeah: man - okay: yeah + two, three, four, yeah: man + okay: yeah -- a += 3 - 5 diff --git a/spec/inputs/tables.moon b/spec/inputs/tables.moon index 10bccde..d7596f5 100644 --- a/spec/inputs/tables.moon +++ b/spec/inputs/tables.moon @@ -1,33 +1,33 @@ backpack = - something: - yeah: 200 - they: -> - print "hello" - yor_feet"small" - pretty: hair - gold: hmm - yow: 1000 + something: + yeah: 200 + they: -> + print "hello" + yor_feet"small" + pretty: hair + gold: hmm + yow: 1000 - eat: goo - yeah: dudd + eat: goo + yeah: dudd start = - something: "cold" + something: "cold" bathe = - on: "fire" + on: "fire" another = - [4]: 232 - ["good food"]: "is the best" + [4]: 232 + ["good food"]: "is the best" fwip = - something: hello"what", number: 2323, - what: yo "momma", "yeah", - fruit: basket - nuts: day + something: hello"what", number: 2323, + what: yo "momma", "yeah", + fruit: basket + nuts: day frick = hello: "world" @@ -38,106 +38,106 @@ ya = { 1,2,3, key: 100, 343, "hello", umm: 232 } x = { 1,2, - 4343, 343 ,343 } + 4343, 343 ,343 } g, p = { - 1,2, nowy: "yes", 3,4, - hey: 232, another: "day" + 1,2, nowy: "yes", 3,4, + hey: 232, another: "day" }, 234 annother = { - 1,2,3 - 3,4,5 - 6,7,8 + 1,2,3 + 3,4,5 + 6,7,8 } yeah = { - [232]: 3434, "helo" - ice: "cake" + [232]: 3434, "helo" + ice: "cake" } -- confusing stuff... whatabout = { - hello world, another - what, about, now + hello world, another + what, about, now - hello"world", yeah - hello "world", yeah + hello"world", yeah + hello "world", yeah } x = - -- yeah - something: => "hello" - cool: -- umm - --so ething - bed: { - 2323,2323 - } - red: 2343 -- here - -- what - name: (node) => @value node -- here - -- comment me + -- yeah + something: => "hello" + cool: -- umm + --so ething + bed: { + 2323,2323 + } + red: 2343 -- here + -- what + name: (node) => @value node -- here + -- comment me -- okay x = { :something, something: something } y = { - :hi, :there, :how, :you - :thing + :hi, :there, :how, :you + :thing } call_me "hello", :x, :y, :z t = { - a: 'a' - [b]: 'b' + a: 'a' + [b]: 'b' } xam = { - hello: 1234 - "hello": 12354 - ["hello"]: 12354 + hello: 1234 + "hello": 12354 + ["hello"]: 12354 } kam = { - hello: 12 - goodcheese: - "mmm" + hello: 12 + goodcheese: + "mmm" - yeah: - 12 + 232 + yeah: + 12 + 232 - lets: - keepit going: true, - okay: "yeah" + lets: + keepit going: true, + okay: "yeah" - more: - { - 1, [x for x=1,10] - } + more: + { + 1, [x for x=1,10] + } - [{"one", "two"}]: - one_thing => + [{"one", "two"}]: + one_thing => } -- TODO: both of these have undesirable output keepit going: true, - okay: "yeah", - workd: "okay" + okay: "yeah", + workd: "okay" thing what: - "great", no: - "more" - okay: 123 + "great", no: + "more" + okay: 123 -- thing what: - "great", no: - "more" + "great", no: + "more" _ = okay: 123 -- a anon table diff --git a/spec/inputs/unless_else.moon b/spec/inputs/unless_else.moon index fe96c0b..b421d4d 100644 --- a/spec/inputs/unless_else.moon +++ b/spec/inputs/unless_else.moon @@ -1,5 +1,5 @@ if a - unless b - print "hi" - elseif c - print "not hi" + unless b + print "hi" + elseif c + print "not hi" diff --git a/spec/inputs/using.moon b/spec/inputs/using.moon index cd78ba6..fe0a433 100644 --- a/spec/inputs/using.moon +++ b/spec/inputs/using.moon @@ -3,20 +3,20 @@ hello = "hello" world = "world" _ = (using nil) -> - hello = 3223 + hello = 3223 _ = (a using nil) -> - hello = 3223 - a = 323 + hello = 3223 + a = 323 _ = (a,b,c using a,b,c) -> - a,b,c = 1,2,3 - world = 12321 + a,b,c = 1,2,3 + world = 12321 (a,e,f using a,b,c, hello) -> - a,b,c = 1,2,3 - hello = 12321 - world = "yeah" + a,b,c = 1,2,3 + hello = 12321 + world = "yeah" diff --git a/spec/inputs/whitespace.moon b/spec/inputs/whitespace.moon index e505b1b..36f14ae 100644 --- a/spec/inputs/whitespace.moon +++ b/spec/inputs/whitespace.moon @@ -1,6 +1,6 @@ _ = { - 1, 2 + 1, 2 } _ = { 1, 2 @@ -16,22 +16,22 @@ _ = { } _ = { something 1,2, - 4,5,6, - 3,4,5 + 4,5,6, + 3,4,5 } _ = { - a 1,2,3, - 4,5,6 - 1,2,3 + a 1,2,3, + 4,5,6 + 1,2,3 } _ = { - b 1,2,3, - 4,5,6 - 1,2,3, - 1,2,3 + b 1,2,3, + 4,5,6 + 1,2,3, + 1,2,3 } _ = { 1,2,3 } @@ -41,61 +41,61 @@ _ = { c 1,2,3, hello 1,2,3,4, - 1,2,3,4,4,5 + 1,2,3,4,4,5 x 1, - 2, 3, - 4, 5, 6 + 2, 3, + 4, 5, 6 hello 1,2,3, - world 4,5,6, - 5,6,7,8 + world 4,5,6, + 5,6,7,8 hello 1,2,3, - world 4,5,6, - 5,6,7,8, - 9,9 + world 4,5,6, + 5,6,7,8, + 9,9 _ = { - hello 1,2, - 3,4, - 5, 6 + hello 1,2, + 3,4, + 5, 6 } x = { - hello 1,2,3,4, - 5,6,7 - 1,2,3,4 + hello 1,2,3,4, + 5,6,7 + 1,2,3,4 } if hello 1,2,3, - world, - world - print "hello" + world, + world + print "hello" if hello 1,2,3, - world, - world - print "hello" + world, + world + print "hello" -- a( - one, two, three + one, two, three ) b( - one, - two, - three + one, + two, + three ) c(one, two, - three, four) + three, four) -- diff --git a/spec/inputs/with.moon b/spec/inputs/with.moon index f543356..704c494 100644 --- a/spec/inputs/with.moon +++ b/spec/inputs/with.moon @@ -1,118 +1,118 @@ do - a = -> - with something - print .hello - print hi - print "world" + a = -> + with something + print .hello + print hi + print "world" do - with leaf - .world! - .world 1,2,3 + with leaf + .world! + .world 1,2,3 - g = .what.is.this + g = .what.is.this - .hi 1,2,3 + .hi 1,2,3 - \hi(1,2).world 2323 + \hi(1,2).world 2323 - \hi "yeah", "man" - .world = 200 + \hi "yeah", "man" + .world = 200 do - zyzyzy = with something - .set_state "hello world" + zyzyzy = with something + .set_state "hello world" do - x = 5 + with Something! - \write "hello world" + x = 5 + with Something! + \write "hello world" do - x = { - hello: with yeah - \okay! - } + x = { + hello: with yeah + \okay! + } do - with foo - _ = \prop"something".hello - .prop\send(one) - .prop\send one + with foo + _ = \prop"something".hello + .prop\send(one) + .prop\send one -- do - with a, b -- b is lost - print .world + with a, b -- b is lost + print .world - mod = with _M = {} - .Thing = "hi" + mod = with _M = {} + .Thing = "hi" - -- operate on a only - with a, b = something, pooh - print .world + -- operate on a only + with a, b = something, pooh + print .world - x = with a, b = 1, 2 - print a + b + x = with a, b = 1, 2 + print a + b - print with a, b = 1, 2 - print a + b + print with a, b = 1, 2 + print a + b - -- assignment lhs must be evaluated in the order they appear - p = with hello!.x, world!.y = 1, 2 - print a + b + -- assignment lhs must be evaluated in the order they appear + p = with hello!.x, world!.y = 1, 2 + print a + b -- do - x = "hello" - with x - x\upper! + x = "hello" + with x + x\upper! do - with k = "jo" - print \upper! + with k = "jo" + print \upper! do - with a,b,c = "", "", "" - print \upper! + with a,b,c = "", "", "" + print \upper! do - a = "bunk" - with a,b,c = "", "", "" - print \upper! + a = "bunk" + with a,b,c = "", "", "" + print \upper! do - with j - print \upper! + with j + print \upper! do - with k.j = "jo" - print \upper! + with k.j = "jo" + print \upper! do - with a - print .b - -- nested `with`s should change the scope correctly - with .c - print .d + with a + print .b + -- nested `with`s should change the scope correctly + with .c + print .d do - with a - -- nested `with`s with assignments should change the scope correctly - with .b = 2 - print .c + with a + -- nested `with`s with assignments should change the scope correctly + with .b = 2 + print .c do - _ = -> - with hi - return .a, .b + _ = -> + with hi + return .a, .b do - with dad - .if "yes" - y = .end.of.function + with dad + .if "yes" + y = .end.of.function diff --git a/src/MoonP/moon_ast.h b/src/MoonP/moon_ast.h index f8d2099..e71afe9 100644 --- a/src/MoonP/moon_ast.h +++ b/src/MoonP/moon_ast.h @@ -565,8 +565,9 @@ AST_END(ExpListAssign) AST_NODE(if_else_line, "if_else_line"_id) ast_ptr condition; + ast_ptr assign; ast_sel elseExpr; - AST_MEMBER(if_else_line, &condition, &elseExpr) + AST_MEMBER(if_else_line, &condition, &assign, &elseExpr) AST_END(if_else_line) AST_NODE(unless_line, "unless_line"_id) diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index b1770fb..4723af4 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp @@ -601,6 +601,7 @@ private: auto ifCond = x->new_ptr(); ifCond->condition.set(if_else_line->condition); + ifCond->assign.set(if_else_line->assign); ifNode->nodes.push_back(ifCond); auto stmt = x->new_ptr(); diff --git a/src/MoonP/moon_parser.cpp b/src/MoonP/moon_parser.cpp index d3c0ed2..6d1b86c 100644 --- a/src/MoonP/moon_parser.cpp +++ b/src/MoonP/moon_parser.cpp @@ -198,7 +198,7 @@ rule IfCond = Exp >> -Assign; rule IfElseIf = -(Break >> *EmptyLine >> CheckIndent) >> key("elseif") >> IfCond >> -key("then") >> Body; rule IfElse = -(Break >> *EmptyLine >> CheckIndent) >> key("else") >> Body; rule If = key("if") >> Seperator >> IfCond >> -key("then") >> Body >> *IfElseIf >> -IfElse; -rule Unless = key("unless") >> Seperator >> IfCond >> -key("then") >> Body >> *IfElseIf >> -IfElse; +rule Unless = key("unless") >> Seperator >> IfCond >> -key("then") >> Body >> *IfElseIf >> -IfElse; rule While = key("while") >> DisableDo >> ensure(Exp, PopDo) >> -key("do") >> Body; @@ -510,7 +510,7 @@ rule SimpleValue = rule ExpListAssign = ExpList >> -(Update | Assign); -rule if_else_line = key("if") >> Exp >> (key("else") >> Exp | default_value); +rule if_else_line = key("if") >> Exp >> -Assign >> (key("else") >> Exp | default_value); rule unless_line = key("unless") >> Exp; rule statement_appendix = (if_else_line | unless_line | CompInner) >> Space; -- cgit v1.2.3-55-g6feb