renamed module_def too module_definition and fixed leading space tests
This commit is contained in:
parent
caec5ee6f0
commit
e7a823bf3e
@ -6,7 +6,7 @@ require_relative "control"
|
||||
require_relative "expression"
|
||||
require_relative "call_site"
|
||||
require_relative "function_definition"
|
||||
require_relative "module_def"
|
||||
require_relative "module_definition"
|
||||
require_relative "operators"
|
||||
|
||||
module Parser
|
||||
@ -31,6 +31,6 @@ module Parser
|
||||
include Operators
|
||||
include ModuleDef
|
||||
|
||||
rule(:root){ (module_def | function_definition | expression | call_site ).repeat }
|
||||
rule(:root){ (module_definition | function_definition | expression | call_site ).repeat }
|
||||
end
|
||||
end
|
||||
|
@ -1,7 +1,7 @@
|
||||
module Parser
|
||||
module ModuleDef
|
||||
include Parslet
|
||||
rule(:module_def) do
|
||||
rule(:module_definition) do
|
||||
keyword_module >> name >> eol >>
|
||||
( (keyword_end.absent? >> root).repeat(1)).as(:module_expressions) >> keyword_end >> newline
|
||||
end
|
@ -5,10 +5,10 @@ class TestFoo < MiniTest::Test
|
||||
|
||||
def test_foo
|
||||
@string_input = <<HERE
|
||||
def foo(x)
|
||||
a = 5
|
||||
end
|
||||
foo( 3 )
|
||||
def foo(x)
|
||||
a = 5
|
||||
end
|
||||
foo( 3 )
|
||||
HERE
|
||||
@should = [0x0,0xb0,0xa0,0xe3,0x3,0x10,0xa0,0xe3,0x2,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0x5,0x20,0xa0,0xe3,0x2,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8]
|
||||
@output = ""
|
||||
|
@ -5,7 +5,7 @@ class TestFunctions < MiniTest::Test
|
||||
|
||||
def test_functions
|
||||
@string_input = <<HERE
|
||||
def minus(a,b)
|
||||
def minus(a,b)
|
||||
a - b
|
||||
end
|
||||
def plus(a, b)
|
||||
@ -19,9 +19,9 @@ class TestFunctions < MiniTest::Test
|
||||
t = times(a, m)
|
||||
a = plus(a,t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
putint(times(7,6))
|
||||
putint(times(7,6))
|
||||
HERE
|
||||
@should = [0x0,0xb0,0xa0,0xe3,0x7,0x10,0xa0,0xe3,0x6,0x20,0xa0,0xe3,0xc,0x0,0x0,0xeb,0x7,0x10,0xa0,0xe1,0x32,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0x2,0x30,0x41,0xe0,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x2,0x30,0x81,0xe0,0x3,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x0,0x0,0x52,0xe3,0x10,0x0,0x0,0xa,0x6,0x0,0x2d,0xe9,0x2,0x10,0xa0,0xe1,0x1,0x20,0xa0,0xe3,0xf0,0xff,0xff,0xeb,0x6,0x0,0xbd,0xe8,0x7,0x30,0xa0,0xe1,0xe,0x0,0x2d,0xe9,0x3,0x20,0xa0,0xe1,0xf3,0xff,0xff,0xeb,0xe,0x0,0xbd,0xe8,0x7,0x40,0xa0,0xe1,0x1e,0x0,0x2d,0xe9,0x4,0x20,0xa0,0xe1,0xea,0xff,0xff,0xeb,0x1e,0x0,0xbd,0xe8,0x7,0x10,0xa0,0xe1,0x0,0x0,0x0,0xea,0x0,0x10,0xa0,0xe3,0x1,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0xa,0x30,0x42,0xe2,0x22,0x21,0x42,0xe0,0x22,0x22,0x82,0xe0,0x22,0x24,0x82,0xe0,0x22,0x28,0x82,0xe0,0xa2,0x21,0xa0,0xe1,0x2,0x41,0x82,0xe0,0x84,0x30,0x53,0xe0,0x1,0x20,0x82,0x52,0xa,0x30,0x83,0x42,0x30,0x30,0x83,0xe2,0x0,0x30,0xc1,0xe5,0x1,0x10,0x41,0xe2,0x0,0x0,0x52,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20]
|
||||
@output = " 42 "
|
||||
|
@ -5,7 +5,7 @@ class TestHello < MiniTest::Test
|
||||
|
||||
def test_hello
|
||||
@string_input = <<HERE
|
||||
putstring( "Hello Raisa, I am crystal" )
|
||||
putstring( "Hello Raisa, I am crystal" )
|
||||
HERE
|
||||
@should = [0x0,0xb0,0xa0,0xe3,0x28,0x10,0x8f,0xe2,0x1c,0x20,0xa0,0xe3,0x2,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x48,0x65,0x6c,0x6c,0x6f,0x20,0x52,0x61,0x69,0x73,0x61,0x2c,0x20,0x49,0x20,0x61,0x6d,0x20,0x63,0x72,0x79,0x73,0x74,0x61,0x6c,0x20,0x20,0x20]
|
||||
@output = "Hello Raisa, I am crystal "
|
||||
|
@ -5,14 +5,15 @@ class TestIf < MiniTest::Test
|
||||
|
||||
def test_if
|
||||
@string_input = <<HERE
|
||||
def itest(n)
|
||||
def itest(n)
|
||||
if( n < 12)
|
||||
putstring("then")
|
||||
else
|
||||
putstring("else")
|
||||
end
|
||||
end
|
||||
itest(20)
|
||||
end
|
||||
|
||||
itest(20)
|
||||
HERE
|
||||
@should = [0x0,0xb0,0xa0,0xe3,0x14,0x10,0xa0,0xe3,0x2,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0xc,0x0,0x51,0xe3,0x5,0x0,0x0,0xba,0x2,0x0,0x2d,0xe9,0x3c,0x10,0x8f,0xe2,0x8,0x20,0xa0,0xe3,0x7,0x0,0x0,0xeb,0x2,0x0,0xbd,0xe8,0x4,0x0,0x0,0xea,0x2,0x0,0x2d,0xe9,0x2c,0x10,0x8f,0xe2,0x8,0x20,0xa0,0xe3,0x1,0x0,0x0,0xeb,0x2,0x0,0xbd,0xe8,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x65,0x6c,0x73,0x65,0x20,0x20,0x20,0x20,0x74,0x68,0x65,0x6e,0x20,0x20,0x20,0x20]
|
||||
@output = "else "
|
||||
|
@ -5,7 +5,7 @@ class TestPutint < MiniTest::Test
|
||||
|
||||
def test_putint
|
||||
@string_input = <<HERE
|
||||
putint( 42 )
|
||||
putint( 42 )
|
||||
HERE
|
||||
@should = [0x0,0xb0,0xa0,0xe3,0x2a,0x10,0xa0,0xe3,0x13,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0xa,0x30,0x42,0xe2,0x22,0x21,0x42,0xe0,0x22,0x22,0x82,0xe0,0x22,0x24,0x82,0xe0,0x22,0x28,0x82,0xe0,0xa2,0x21,0xa0,0xe1,0x2,0x41,0x82,0xe0,0x84,0x30,0x53,0xe0,0x1,0x20,0x82,0x52,0xa,0x30,0x83,0x42,0x30,0x30,0x83,0xe2,0x0,0x30,0xc1,0xe5,0x1,0x10,0x41,0xe2,0x0,0x0,0x52,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20]
|
||||
@output = " 42 "
|
||||
|
@ -5,7 +5,7 @@ class TestRecursinveFibo < MiniTest::Test
|
||||
|
||||
def test_recursive_fibo
|
||||
@string_input = <<HERE
|
||||
def fibonaccir( n )
|
||||
def fibonaccir( n )
|
||||
if( n <= 1 )
|
||||
return n
|
||||
else
|
||||
@ -13,9 +13,9 @@ class TestRecursinveFibo < MiniTest::Test
|
||||
b = fibonaccir( n - 2 )
|
||||
return a + b
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
putint(fibonaccir( 10 ))
|
||||
putint(fibonaccir( 10 ))
|
||||
HERE
|
||||
@should = [0x0,0xb0,0xa0,0xe3,0xa,0x10,0xa0,0xe3,0x4,0x0,0x0,0xeb,0x7,0x10,0xa0,0xe1,0x27,0x0,0x0,0xeb,0x1,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x40,0x2d,0xe9,0x1,0x0,0x51,0xe3,0xe,0x0,0x0,0xda,0x1,0x20,0x41,0xe2,0x6,0x0,0x2d,0xe9,0x2,0x10,0xa0,0xe1,0xf8,0xff,0xff,0xeb,0x6,0x0,0xbd,0xe8,0x7,0x30,0xa0,0xe1,0x2,0x40,0x41,0xe2,0x1e,0x0,0x2d,0xe9,0x4,0x10,0xa0,0xe1,0xf2,0xff,0xff,0xeb,0x1e,0x0,0xbd,0xe8,0x7,0x50,0xa0,0xe1,0x5,0x60,0x83,0xe0,0x6,0x70,0xa0,0xe1,0x0,0x0,0x0,0xea,0x1,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0xa,0x30,0x42,0xe2,0x22,0x21,0x42,0xe0,0x22,0x22,0x82,0xe0,0x22,0x24,0x82,0xe0,0x22,0x28,0x82,0xe0,0xa2,0x21,0xa0,0xe1,0x2,0x41,0x82,0xe0,0x84,0x30,0x53,0xe0,0x1,0x20,0x82,0x52,0xa,0x30,0x83,0x42,0x30,0x30,0x83,0xe2,0x0,0x30,0xc1,0xe5,0x1,0x10,0x41,0xe2,0x0,0x0,0x52,0xe3,0xef,0xff,0xff,0x1b,0x0,0x80,0xbd,0xe8,0x0,0x40,0x2d,0xe9,0x1,0x20,0xa0,0xe1,0x20,0x10,0x8f,0xe2,0x9,0x10,0x81,0xe2,0xe9,0xff,0xff,0xeb,0x14,0x10,0x8f,0xe2,0xc,0x20,0xa0,0xe3,0x1,0x0,0xa0,0xe3,0x4,0x70,0xa0,0xe3,0x0,0x0,0x0,0xef,0x0,0x70,0xa0,0xe1,0x0,0x80,0xbd,0xe8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20]
|
||||
@output = " 55 "
|
||||
|
@ -5,7 +5,7 @@ class TestWhileFragment < MiniTest::Test
|
||||
|
||||
def test_while
|
||||
@string_input = <<HERE
|
||||
def fibonaccit(n) # n == r0
|
||||
def fibonaccit(n) # n == r0
|
||||
a = 0 # a == r1
|
||||
b = 1 # b = r2
|
||||
while( n > 1 ) do #BUG comment lines + comments behind function calls
|
||||
@ -15,7 +15,7 @@ class TestWhileFragment < MiniTest::Test
|
||||
n = n - 1 # r0 <- r2 for call, #call ok
|
||||
end #r5 <- r0 - 1 n=n-1 through r5 tmp
|
||||
return b
|
||||
end # r0 <- r5
|
||||
end # r0 <- r5
|
||||
|
||||
putint(fibonaccit( 10 ))
|
||||
HERE
|
||||
|
@ -12,7 +12,7 @@ end
|
||||
HERE
|
||||
@parse_output = {:name=>"foo", :module_expressions=>[{:integer=>"5"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new("foo" ,[Ast::IntegerExpression.new(5)] )
|
||||
@parser = @parser.module_def
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
def test_module_ops
|
||||
@ -26,7 +26,7 @@ end
|
||||
HERE
|
||||
@parse_output = {:name=>"ops", :module_expressions=>[{:function_name=>{:name=>"foo"}, :parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new("ops" ,[Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("abba"),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] )] )
|
||||
@parser = @parser.module_def
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
def test_module_if
|
||||
@ -43,7 +43,7 @@ end
|
||||
HERE
|
||||
@parse_output = {:name=>"sif", :module_expressions=>[{:function_name=>{:name=>"ofthen"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}], :expressions=>[{:if=>"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:l=>{:name=>"isit"}, :o=>"= ", :r=>{:integer=>"42"}}], :else=>"else"}, :if_false=>{:expressions=>[{:l=>{:name=>"maybenot"}, :o=>"= ", :r=>{:integer=>"667"}}], :end=>"end"}}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new("sif" ,[Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new("n")] , [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::OperatorExpression.new("=", Ast::NameExpression.new("isit"),Ast::IntegerExpression.new(42))],[Ast::OperatorExpression.new("=", Ast::NameExpression.new("maybenot"),Ast::IntegerExpression.new(667))] )] )] )
|
||||
@parser = @parser.module_def
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
def test_module_function
|
||||
@ -57,6 +57,6 @@ end
|
||||
HERE
|
||||
@parse_output = {:name=>"sif", :module_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}, {:function_name=>{:name=>"ofthen"}, :parmeter_list=>[{:parmeter=>{:name=>"n"}}, {:parmeter=>{:name=>"m"}}], :expressions=>[{:integer=>"44"}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:sif ,[Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new("var")] ), Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new("n"),Ast::NameExpression.new("m")] , [Ast::IntegerExpression.new(44)] )] )
|
||||
@parser = @parser.module_def
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user