fix all tests
This commit is contained in:
parent
3b484aa8ff
commit
b938c7ace5
@ -2,19 +2,31 @@ module Parser
|
||||
# Basic types are numbers and strings
|
||||
module BasicTypes
|
||||
include Parslet
|
||||
# space really is just space. ruby is newline sensitive, so there is more whitespace footwork
|
||||
|
||||
# unicode generalized categories , according to regex ruby page
|
||||
rule(:lower) { match "[[:lower:]]" } # Lowercase alphabetical character
|
||||
rule(:upper) { match "[[:upper:]]" } # Uppercase alphabetical
|
||||
rule(:alnum) { match "[[:alnum:]]" } # Alphabetic and numeric character
|
||||
rule(:alpha) { match "[[:alpha:]]" } # Alphabetic character
|
||||
rule(:blank) { match "[[:blank:]]" } # Space or tab
|
||||
rule(:space) { match("[[:space:]]").repeat } # Whitespace character ([:blank:], newline, carriage return, etc.)
|
||||
rule(:digit) { match "[[:digit:]]" } # Digit
|
||||
rule(:graph) { match "[[:graph:]]" } # Non-blank character (excludes spaces, control characters, and similar)
|
||||
rule(:print) { match "[[:print:]]" } # Like [:graph:], but includes the space character
|
||||
rule(:xdigit) { match "[[:xdigit:]]"} # Digit allowed in a hexadecimal number (i.e., 0-9a-fA-F)
|
||||
|
||||
|
||||
# rule of thumb is that anything eats space behind it, but only space, no newlines
|
||||
rule(:space) { (str('\t') | str(' ')).repeat(1) }
|
||||
rule(:space?) { space.maybe }
|
||||
rule(:linebreak){ str("\n") >> space? >> linebreak.repeat }
|
||||
|
||||
|
||||
rule(:quote) { str('"') }
|
||||
rule(:nonquote) { str('"').absent? >> any }
|
||||
|
||||
rule(:comment){ match('#') >> (linebreak.absent? >> any).repeat >> linebreak }
|
||||
rule(:newline) { linebreak | comment }
|
||||
rule(:newline) { (linebreak | comment) >> space? }
|
||||
rule(:eol) { newline | any.absent? }
|
||||
|
||||
|
||||
rule(:double_quote){ str('"') }
|
||||
rule(:minus) { str('-') }
|
||||
rule(:plus) { str('+') }
|
||||
@ -23,27 +35,28 @@ module Parser
|
||||
rule(:dot) { str('.') }
|
||||
rule(:digit) { match('[0-9]') }
|
||||
rule(:exponent) { (str('e')| str('E')) }
|
||||
|
||||
|
||||
rule(:type) { (str("int") | str("ref")).as(:type) >> space? }
|
||||
# identifier must start with lower case
|
||||
# TODO rule forbit names like if_true, because it starts with a keyword. a little looser please!
|
||||
rule(:name) { keyword.absent? >> (match['a-z_'] >> match['a-zA-Z0-9_'].repeat).as(:name) >> space? }
|
||||
# instance variables must have the @
|
||||
rule(:instance_variable) { (str('@') >> name).as(:instance_variable) }
|
||||
# and class/module names must start with capital
|
||||
# fields have type
|
||||
rule(:field) { type >> name }
|
||||
# and class/module names must start with capital
|
||||
# (admittatly the rule matches constants too, but one step at a time)
|
||||
rule(:module_name) { keyword.absent? >> (match['A-Z'] >> match['a-zA-Z0-9_'].repeat).as(:module_name) >> space? }
|
||||
|
||||
|
||||
rule(:escape) { str('\\') >> any.as(:esc) }
|
||||
rule(:string) { quote >> (
|
||||
escape |
|
||||
escape |
|
||||
nonquote.as(:char)
|
||||
).repeat(1).as(:string) >> quote }
|
||||
|
||||
|
||||
rule(:integer) { sign.maybe >> digit.repeat(1).as(:integer) >> space? }
|
||||
|
||||
rule(:float) { integer >> dot >> integer >>
|
||||
|
||||
rule(:float) { integer >> dot >> integer >>
|
||||
(exponent >> sign.maybe >> digit.repeat(1,3)).maybe >> space?}
|
||||
rule(:basic_type){ integer | name | string | float | instance_variable | module_name |
|
||||
rule(:basic_type){ integer | name | string | float | field | module_name |
|
||||
keyword_true | keyword_false | keyword_nil }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -9,9 +9,9 @@ module Parser
|
||||
space? >> right_parenthesis
|
||||
}
|
||||
|
||||
rule(:call_site) { ((module_name|instance_variable|basic_type).as(:receiver) >> str(".")).maybe >> #possibly qualified
|
||||
rule(:call_site) { ((module_name|name|basic_type).as(:receiver) >> str(".")).maybe >> #possibly qualified
|
||||
name.as(:call_site) >> argument_list >> comment.maybe}
|
||||
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -2,16 +2,20 @@ module Parser
|
||||
module Control
|
||||
include Parslet
|
||||
rule(:conditional) do
|
||||
keyword_if >>
|
||||
(( (value_expression|operator_expression).as(:conditional) ) |
|
||||
left_parenthesis >> (operator_expression|value_expression).as(:conditional) >> right_parenthesis) >>
|
||||
newline >> expressions_else.as(:if_true) >> newline >> expressions_end.as(:if_false)
|
||||
keyword_if >>
|
||||
left_parenthesis >> (operator_expression|value_expression).as(:conditional) >> right_parenthesis >>
|
||||
expressions_else.as(:if_true) >> expressions_end.as(:if_false)
|
||||
end
|
||||
|
||||
rule(:small_conditional) do
|
||||
keyword_if >>
|
||||
left_parenthesis >> (operator_expression|value_expression).as(:conditional) >> right_parenthesis >>
|
||||
expressions_end.as(:if_true)
|
||||
end
|
||||
|
||||
|
||||
rule(:while_do) do
|
||||
keyword_while >> left_parenthesis >> (operator_expression|value_expression).as(:while_cond) >>
|
||||
right_parenthesis >> keyword_do >> newline >>
|
||||
expressions_end.as(:body)
|
||||
right_parenthesis >> expressions_end.as(:body)
|
||||
end
|
||||
rule(:simple_return) do
|
||||
keyword_return >> (operator_expression|value_expression).as(:return_expression)
|
||||
|
@ -1,10 +1,10 @@
|
||||
module Parser
|
||||
module Expression
|
||||
include Parslet
|
||||
|
||||
|
||||
rule(:value_expression) { call_site | basic_type }
|
||||
|
||||
rule(:expression) { (simple_return | while_do | conditional | operator_expression | call_site ) >> newline }
|
||||
rule(:expression) { (simple_return | while_do | small_conditional | conditional | operator_expression | call_site ) }
|
||||
|
||||
def delimited_expressions( delimit )
|
||||
( (delimit.absent? >> expression).repeat(1)).as(:expressions) >> delimit
|
||||
|
@ -1,10 +1,10 @@
|
||||
module Parser
|
||||
module FunctionDefinition
|
||||
include Parslet
|
||||
|
||||
|
||||
rule(:function_definition) {
|
||||
keyword_def >> ((module_name|instance_variable|name).as(:receiver) >> str(".")).maybe >> #possibly qualified
|
||||
name.as(:function_name) >> parameter_list.maybe >> newline >> expressions_end >> newline
|
||||
keyword_def >> ((module_name|name).as(:receiver) >> str(".")).maybe >> #possibly qualified
|
||||
name.as(:function_name) >> parameter_list.maybe >> space >> expressions_end >> space?
|
||||
}
|
||||
|
||||
rule(:parameter_list) {
|
||||
|
@ -1,28 +1,28 @@
|
||||
module Parser
|
||||
module Keywords
|
||||
include Parslet
|
||||
|
||||
|
||||
rule(:keyword_begin) { str('begin').as(:begin) >> space?}
|
||||
rule(:keyword_class) { str('class') >> space? }
|
||||
rule(:keyword_def) { str('def') >> space? }
|
||||
rule(:keyword_do) { str('do').as(:do) >> space?}
|
||||
rule(:keyword_else) { str('else').as(:else) >> space? }
|
||||
rule(:keyword_end) { str('end').as(:end) >> space? }
|
||||
rule(:keyword_false) { str('false').as(:false) >> space?}
|
||||
rule(:keyword_if) { str('if').as(:if) >> space? }
|
||||
rule(:keyword_end) { str('end').as(:end) >> space? }
|
||||
rule(:keyword_false) { str('false').as(:false) }
|
||||
rule(:keyword_if) { str('if').as(:if) }
|
||||
rule(:keyword_rescue) { str('rescue').as(:rescue) >> space?}
|
||||
rule(:keyword_return) { str('return').as(:return) >> space?}
|
||||
rule(:keyword_true) { str('true').as(:true) >> space?}
|
||||
rule(:keyword_true) { str('true').as(:true) }
|
||||
rule(:keyword_module) { str('module') >> space? }
|
||||
rule(:keyword_nil) { str('nil').as(:nil) >> space?}
|
||||
rule(:keyword_nil) { str('nil').as(:nil) }
|
||||
rule(:keyword_unless) { str('unless').as(:unless) >> space?}
|
||||
rule(:keyword_until) { str('until').as(:until) >> space?}
|
||||
rule(:keyword_while) { str('while').as(:while) >> space?}
|
||||
|
||||
# this rule is just to make sure identifiers can't be keywords. Kind of duplication here, but we need the
|
||||
rule(:keyword_while) { str('while').as(:while) }
|
||||
|
||||
# this rule is just to make sure identifiers can't be keywords. Kind of duplication here, but we need the
|
||||
# space in above rules, so just make sure to add any here too.
|
||||
rule(:keyword){ str('begin') | str('def') | str('do') | str('else') | str('end') |
|
||||
rule(:keyword){ str('begin') | str('def') | str('do') | str('else') | str('end') |
|
||||
str('false')| str('if')| str('rescue')| str('true')| str('nil') |
|
||||
str('unless')| str('until')| str('while')}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -2,13 +2,13 @@ module Parser
|
||||
module ModuleDef
|
||||
include Parslet
|
||||
rule(:module_definition) do
|
||||
keyword_module >> module_name >> eol >>
|
||||
( (keyword_end.absent? >> root_body).repeat()).as(:module_expressions) >> keyword_end >> newline
|
||||
keyword_module >> module_name >>
|
||||
( (keyword_end.absent? >> root_body).repeat()).as(:module_expressions) >> keyword_end
|
||||
end
|
||||
|
||||
rule(:class_definition) do
|
||||
keyword_class >> module_name >> (smaller >> module_name).maybe.as(:derived_name) >> eol >>
|
||||
( (keyword_end.absent? >> root_body).repeat()).as(:class_expressions) >> keyword_end >> newline
|
||||
keyword_class >> module_name >> (smaller >> module_name).maybe.as(:derived_name) >>
|
||||
( (keyword_end.absent? >> root_body).repeat()).as(:class_expressions) >> keyword_end
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -20,8 +20,8 @@ module Parser
|
||||
rule(:op_assign) { str('+=')|str('-=')|str('*=')|str('/=')|str('%=') >> space?}
|
||||
rule(:eclipse) { str('..') |str("...") >> space?}
|
||||
rule(:assign) { str('=') >> space?}
|
||||
|
||||
#infix doing the heavy lifting here,
|
||||
|
||||
#infix doing the heavy lifting here,
|
||||
# is defined as an expressions and array of [atoms,priority,binding] triples
|
||||
rule(:operator_expression) do infix_expression(value_expression,
|
||||
[exponent, 120, :left] ,
|
||||
@ -40,13 +40,9 @@ module Parser
|
||||
[boolean_and, 60, :left],
|
||||
[boolean_or, 50, :right],
|
||||
[eclipse, 40, :right],
|
||||
[keyword_rescue, 30, :right],
|
||||
[keyword_rescue, 30, :right],
|
||||
[assign, 20, :right],
|
||||
[op_assign, 20, :right],
|
||||
[keyword_until, 10, :right],
|
||||
[keyword_while, 10, :right],
|
||||
[keyword_unless, 10, :right],
|
||||
[keyword_if, 10, :right])
|
||||
[op_assign, 20, :right] ) >> space?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,53 +1,61 @@
|
||||
require 'parslet'
|
||||
require 'ast/expression'
|
||||
require "ast"
|
||||
|
||||
Parslet::Context.include AST::Sexp
|
||||
|
||||
module Parser
|
||||
class Transform < Parslet::Transform
|
||||
rule(:string => sequence(:chars)) { Ast::StringExpression.new chars.join }
|
||||
|
||||
rule(:string => sequence(:chars)) { s(:string , chars.join) }
|
||||
rule(:esc => simple(:esc)) { '\\' + esc }
|
||||
rule(char: simple(:char)) { char }
|
||||
|
||||
rule(:true => simple(:true)) { Ast::TrueExpression.new() }
|
||||
rule(:false => simple(:false)) { Ast::FalseExpression.new() }
|
||||
rule(:nil => simple(:nil)) { Ast::NilExpression.new() }
|
||||
rule(:integer => simple(:value)) { Ast::IntegerExpression.new(value.to_i) }
|
||||
rule(:name => simple(:name)) { Ast::NameExpression.new(name.to_s) }
|
||||
rule(:instance_variable => simple(:instance_variable)) { Ast::VariableExpression.new(instance_variable.name) }
|
||||
rule(:module_name => simple(:module_name)) { Ast::ModuleName.new(module_name.to_s) }
|
||||
rule(:true => simple(:true)) { s(:true) }
|
||||
rule(:false => simple(:false)) { s(:false) }
|
||||
rule(:nil => simple(:nil)) { s(:nil) }
|
||||
rule(:integer => simple(:value)) { s(:int ,value.to_i) }
|
||||
rule(:name => simple(:name)) { s(:name , name.to_s) }
|
||||
rule(:type => simple(:type), :name => simple(:name)) { s(:field , type.to_sym , name.to_sym) }
|
||||
rule(:module_name => simple(:module_name)) { s(:module,module_name.to_s) }
|
||||
|
||||
rule(:array_constant => sequence(:array_constant) ) { Ast::ArrayExpression.new(array_constant) }
|
||||
rule(:array_constant => sequence(:array_constant) ) { s(:array , array_constant) }
|
||||
rule(:array_element => simple(:array_element)) { array_element }
|
||||
rule(:hash_constant => sequence(:hash_constant) ) { Ast::HashExpression.new(hash_constant) }
|
||||
rule(:hash_key => simple(:hash_key) , :hash_value => simple(:hash_value)) { Ast::AssociationExpression.new(hash_key,hash_value) }
|
||||
rule(:hash_constant => sequence(:hash_constant) ) { s(:hash , hash_constant) }
|
||||
rule(:hash_key => simple(:hash_key) , :hash_value => simple(:hash_value)) { s(:assoc , hash_key , hash_value) }
|
||||
rule(:hash_pair => simple(:hash_pair) ) { hash_pair }
|
||||
|
||||
rule(:argument => simple(:argument)) { argument }
|
||||
rule(:argument_list => sequence(:argument_list)) { argument_list }
|
||||
|
||||
#Two rules for calls, simple and qualified. Keeps the rules simpler
|
||||
rule( :call_site => simple(:call_site),
|
||||
rule( :call_site => simple(:call_site),
|
||||
:argument_list => sequence(:argument_list)) do
|
||||
Ast::CallSiteExpression.new(call_site.name, argument_list )
|
||||
s(:call , call_site, argument_list )
|
||||
end
|
||||
rule( :receiver => simple(:receiver) , :call_site => simple(:call_site),
|
||||
rule( :receiver => simple(:receiver) , :call_site => simple(:call_site),
|
||||
:argument_list => sequence(:argument_list)) do
|
||||
Ast::CallSiteExpression.new(call_site.name, argument_list , receiver)
|
||||
s(:call , call_site, argument_list , receiver)
|
||||
end
|
||||
|
||||
rule(:if => simple(:if), :conditional => simple(:conditional),
|
||||
:if_true => {:expressions => sequence(:if_true) , :else => simple(:else) },
|
||||
:if_false => {:expressions => sequence(:if_false) , :end => simple(:e) }) do
|
||||
Ast::IfExpression.new(conditional, if_true, if_false)
|
||||
end
|
||||
s(:if , conditional, if_true, if_false)
|
||||
end
|
||||
|
||||
rule(:if => simple(:if), :conditional => simple(:conditional),
|
||||
:if_true => {:expressions => sequence(:if_true) , :end => simple(:e) }) do
|
||||
s(:if , conditional, if_true, nil)
|
||||
end
|
||||
|
||||
rule(:while => simple(:while),
|
||||
:while_cond => simple(:while_cond) , :do => simple(:do) ,
|
||||
:while_cond => simple(:while_cond) ,
|
||||
:body => {:expressions => sequence(:body) , :end => simple(:e) }) do
|
||||
Ast::WhileExpression.new(while_cond, body)
|
||||
s(:while , while_cond, body)
|
||||
end
|
||||
|
||||
rule(:return => simple(:return) , :return_expression => simple(:return_expression))do
|
||||
Ast::ReturnExpression.new(return_expression)
|
||||
s(:return , return_expression)
|
||||
end
|
||||
|
||||
rule(:parameter => simple(:parameter)) { parameter }
|
||||
@ -57,43 +65,43 @@ module Parser
|
||||
rule(:function_name => simple(:function_name),
|
||||
:parameter_list => sequence(:parameter_list),
|
||||
:expressions => sequence(:expressions) , :end => simple(:e)) do
|
||||
Ast::FunctionExpression.new(function_name.name, parameter_list, expressions)
|
||||
s(:function, function_name, parameter_list, expressions)
|
||||
end
|
||||
|
||||
rule(:function_name => simple(:function_name),
|
||||
:expressions => sequence(:expressions) , :end => simple(:e)) do
|
||||
Ast::FunctionExpression.new(function_name.name, [], expressions)
|
||||
s(:function , function_name, [], expressions)
|
||||
end
|
||||
|
||||
rule(:receiver=> simple(:receiver),
|
||||
:function_name => simple(:function_name),
|
||||
:parameter_list => sequence(:parameter_list),
|
||||
:expressions => sequence(:expressions) , :end => simple(:e)) do
|
||||
Ast::FunctionExpression.new(function_name.name, parameter_list, expressions , receiver)
|
||||
s(:function, function_name, parameter_list, expressions , receiver)
|
||||
end
|
||||
|
||||
rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do
|
||||
rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do
|
||||
op = o.to_s.strip
|
||||
if op == "="
|
||||
Ast::AssignmentExpression.new( l ,r)
|
||||
s(:assign , l ,r)
|
||||
else
|
||||
Ast::OperatorExpression.new( op , l ,r)
|
||||
s(:operator, op , l ,r)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
#modules and classes are understandibly quite similar Class < Module
|
||||
rule( :module_name => simple(:module_name) , :module_expressions => sequence(:module_expressions) , :end=>"end") do
|
||||
Ast::ModuleExpression.new(module_name , module_expressions)
|
||||
s(:module , module_name , module_expressions)
|
||||
end
|
||||
rule( :module_name => simple(:module_name) , :derived_name => simple(:derived_name) , :class_expressions => sequence(:class_expressions) , :end=>"end") do
|
||||
Ast::ClassExpression.new(module_name , derived_name ? derived_name.name : nil , class_expressions)
|
||||
s(:class , module_name , derived_name ? derived_name : nil , class_expressions)
|
||||
end
|
||||
|
||||
|
||||
rule(:expression_list => sequence(:expression_list)) {
|
||||
Ast::ExpressionList.new(expression_list)
|
||||
s(:list , expression_list)
|
||||
}
|
||||
#shortcut to get the ast tree for a given string
|
||||
# optional second arguement specifies a rule that will be parsed (mainly for testing)
|
||||
# optional second arguement specifies a rule that will be parsed (mainly for testing)
|
||||
def self.ast string , rule = :root
|
||||
syntax = Parser.new.send(rule).parse(string)
|
||||
tree = Transform.new.apply(syntax)
|
||||
|
@ -9,12 +9,13 @@ Parslet::Slice.class_eval do
|
||||
end
|
||||
# Included in parser test will create tests methods
|
||||
module ParserHelper
|
||||
|
||||
|
||||
def self.included(base)
|
||||
base.send :include, InstanceMethods #provides helpers and setup
|
||||
base.send :include, AST::Sexp
|
||||
base.send :extend, ClassMethods #gets the method creation going
|
||||
end
|
||||
|
||||
|
||||
module InstanceMethods
|
||||
def setup
|
||||
@parser = Parser::Salama.new
|
||||
@ -44,7 +45,7 @@ module ParserHelper
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# this creates test methods dynamically. For each test_* method we create
|
||||
# this creates test methods dynamically. For each test_* method we create
|
||||
# three test_*[ast/parse/transf] methods that in turn check the three phases.
|
||||
# runnable_methods is called by minitest to determine which tests to run
|
||||
def runnable_methods
|
||||
|
@ -3,41 +3,35 @@ require_relative "../parser_helper"
|
||||
class RootTestBasic < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_number
|
||||
@string_input = '42 '
|
||||
@parse_output = {:expression_list=>[{:integer=>"42"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::IntegerExpression.new(42)])
|
||||
@transform_output = s(:list, [s(:int, 42)])
|
||||
end
|
||||
|
||||
def test_name
|
||||
@string_input = 'foo '
|
||||
@parse_output = {:expression_list=>[{:name=>"foo"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::NameExpression.new(:foo)])
|
||||
@transform_output = s(:list, [s(:name, "foo")])
|
||||
end
|
||||
|
||||
def test_name_underscode_start
|
||||
@string_input = '_bar '
|
||||
@parse_output = {:expression_list=>[{:name=>"_bar"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::NameExpression.new(:_bar)])
|
||||
@transform_output = s(:list, [s(:name, "_bar")])
|
||||
end
|
||||
|
||||
def test_name_underscode_middle
|
||||
@string_input = 'foo_bar '
|
||||
@parse_output = {:expression_list=>[{:name=>"foo_bar"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::NameExpression.new(:foo_bar)])
|
||||
end
|
||||
|
||||
def test_instance_variable
|
||||
@string_input = '@foo_bar '
|
||||
@parse_output = {:expression_list=>[{:instance_variable=>{:name=>"foo_bar"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::VariableExpression.new(:foo_bar)])
|
||||
@transform_output = s(:list, [s(:name, "foo_bar")])
|
||||
end
|
||||
|
||||
def test_module_name
|
||||
@string_input = 'FooBar '
|
||||
@parse_output = {:expression_list=>[{:module_name=>"FooBar"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ModuleName.new(:FooBar)])
|
||||
@transform_output = s(:list, [s(:module, "FooBar")])
|
||||
end
|
||||
|
||||
def ttest_comment
|
||||
@ -50,14 +44,14 @@ class RootTestBasic < MiniTest::Test
|
||||
def test_string
|
||||
@string_input = "\"hello\""
|
||||
@parse_output = {:expression_list=>[{:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::StringExpression.new("hello")])
|
||||
@transform_output = s(:list, [s(:string, "hello")])
|
||||
end
|
||||
|
||||
def test_string_escapes
|
||||
out = 'hello \nyou'
|
||||
@string_input = '"' + out + '"'
|
||||
@parse_output = {:expression_list=>[{:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}, {:char=>" "}, {:char=>" "}, {:esc=>"n"}, {:char=>"y"}, {:char=>"o"}, {:char=>"u"}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::StringExpression.new(out)])
|
||||
@transform_output = s(:list, [s(:string, out)])
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -1,33 +0,0 @@
|
||||
require_relative "../parser_helper"
|
||||
|
||||
class RootTestBlock < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
def pest_block
|
||||
@string_input = <<HERE
|
||||
self.call(1) do |a , b|
|
||||
tmp = a
|
||||
puts(b)
|
||||
end
|
||||
HERE
|
||||
@parse_output = nil
|
||||
@transform_output = nil
|
||||
@parser = @parser
|
||||
end
|
||||
|
||||
def pest_big_block
|
||||
@string_input = <<HERE
|
||||
self.call( true , false) do | a , b |
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp + b
|
||||
puts(b)
|
||||
n = n - 1
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::BlockExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::NameExpression.new(:b)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:b))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)))] )])
|
||||
@parser = @parser
|
||||
end
|
||||
end
|
@ -3,109 +3,90 @@ require_relative "../parser_helper"
|
||||
class RootTestCallSite < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_single_argument
|
||||
@string_input = 'foo(42)'
|
||||
@parse_output = {:expression_list=>[{:call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::NameExpression.new(:self))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "foo"), [s(:int, 42)])])
|
||||
end
|
||||
|
||||
def test_single_self
|
||||
@string_input = 'self.foo(42)'
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:name=>"self"}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::NameExpression.new(:self))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "foo"), [s(:int, 42)], s(:name, "self"))])
|
||||
end
|
||||
|
||||
def test_single_instance
|
||||
@string_input = '@var.foo(42)'
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:instance_variable=>{:name=>"var"}}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::VariableExpression.new(:var))])
|
||||
@parser = @parser
|
||||
end
|
||||
|
||||
def test_single_name
|
||||
@string_input = 'my_my.foo(42)'
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:name=>"my_my"}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::NameExpression.new(:my_my))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "foo"), [s(:int, 42)], s(:name, "my_my"))])
|
||||
end
|
||||
|
||||
def test_int_receiver
|
||||
@string_input = '42.put()'
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:integer=>"42"}, :call_site=>{:name=>"put"}, :argument_list=>[]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:put, [] ,Ast::IntegerExpression.new(42))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "put"), [], s(:int, 42))])
|
||||
end
|
||||
|
||||
def test_string_receiver
|
||||
@string_input = '"hello".puts()'
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}, :call_site=>{:name=>"puts"}, :argument_list=>[]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:puts, [] ,Ast::StringExpression.new("hello"))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "puts"), [], s(:string, "hello"))])
|
||||
end
|
||||
|
||||
def test_single_class
|
||||
@string_input = 'Object.foo(42)'
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:module_name=>"Object"}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::ModuleName.new(:Object))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "foo"), [s(:int, 42)], s(:module, "Object"))])
|
||||
end
|
||||
|
||||
def test_call_site_multi
|
||||
@string_input = 'baz(42, foo)'
|
||||
@parse_output = {:expression_list=>[{:call_site=>{:name=>"baz"}, :argument_list=>[{:argument=>{:integer=>"42"}}, {:argument=>{:name=>"foo"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:baz, [Ast::IntegerExpression.new(42),Ast::NameExpression.new(:foo)] ,Ast::NameExpression.new(:self))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "baz"), [s(:int, 42), s(:name, "foo")])])
|
||||
end
|
||||
|
||||
def test_call_site_string
|
||||
@string_input = 'puts( "hello")'
|
||||
@parse_output = {:expression_list=>[{:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:puts, [Ast::StringExpression.new("hello")] ,Ast::NameExpression.new(:self))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "puts"), [s(:string, "hello")])])
|
||||
end
|
||||
|
||||
def test_call_operator
|
||||
@string_input = 'puts( 3 + 5)'
|
||||
@parse_output = {:expression_list=>[{:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"5"}}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:puts, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(5))] ,Ast::NameExpression.new(:self))])
|
||||
@transform_output = s(:list, [s(:call, s(:name, "puts"), [s(:operator, "+", s(:int, 3), s(:int, 5))])])
|
||||
@parser = @parser
|
||||
end
|
||||
|
||||
def test_call_two_operators
|
||||
@string_input = 'puts(3 + 5 , a - 3)'
|
||||
@parse_output = {:expression_list=>[{:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"5"}}}, {:argument=>{:l=>{:name=>"a"}, :o=>"- ", :r=>{:integer=>"3"}}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:puts, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::IntegerExpression.new(3))] ,Ast::NameExpression.new(:self))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "puts"), [s(:operator, "+", s(:int, 3), s(:int, 5)), s(:operator, "-", s(:name, "a"), s(:int, 3))])])
|
||||
end
|
||||
|
||||
def test_call_chaining
|
||||
@string_input = 'puts(putint(3 + 5 ), a - 3)'
|
||||
@parse_output = {:expression_list=>[{:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:call_site=>{:name=>"putint"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"5"}}}]}}, {:argument=>{:l=>{:name=>"a"}, :o=>"- ", :r=>{:integer=>"3"}}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:puts, [Ast::CallSiteExpression.new(:putint, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(5))] ,Ast::NameExpression.new(:self)),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::IntegerExpression.new(3))] ,Ast::NameExpression.new(:self))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "puts"), [s(:call, s(:name, "putint"), [s(:operator, "+", s(:int, 3), s(:int, 5))]), s(:operator, "-", s(:name, "a"), s(:int, 3))])])
|
||||
end
|
||||
|
||||
def test_call_chaining_name
|
||||
@string_input = 'puts(name.putint(4), a)'
|
||||
@parse_output = {:expression_list=>[{:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:receiver=>{:name=>"name"}, :call_site=>{:name=>"putint"}, :argument_list=>[{:argument=>{:integer=>"4"}}]}}, {:argument=>{:name=>"a"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:puts, [Ast::CallSiteExpression.new(:putint, [Ast::IntegerExpression.new(4)] ,Ast::NameExpression.new(:name)),Ast::NameExpression.new(:a)] ,Ast::NameExpression.new(:self))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "puts"), [s(:call, s(:name, "putint"), [s(:int, 4)], s(:name, "name")), s(:name, "a")])])
|
||||
end
|
||||
|
||||
def test_call_chaining_class
|
||||
@string_input = 'Class.new(self.get(4))'
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:module_name=>"Class"}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:receiver=>{:name=>"self"}, :call_site=>{:name=>"get"}, :argument_list=>[{:argument=>{:integer=>"4"}}]}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:new, [Ast::CallSiteExpression.new(:get, [Ast::IntegerExpression.new(4)] ,Ast::NameExpression.new(:self))] ,Ast::ModuleName.new(:Class))])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:call, s(:name, "new"), [s(:call, s(:name, "get"), [s(:int, 4)], s(:name, "self"))], s(:module, "Class"))])
|
||||
end
|
||||
def test_call_chaining_instance
|
||||
@string_input = '@class.new(self.get(4))'
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:instance_variable=>{:name=>"class"}}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:receiver=>{:name=>"self"}, :call_site=>{:name=>"get"}, :argument_list=>[{:argument=>{:integer=>"4"}}]}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::CallSiteExpression.new(:new, [Ast::CallSiteExpression.new(:get, [Ast::IntegerExpression.new(4)] ,Ast::NameExpression.new(:self))] ,Ast::VariableExpression.new(:class))])
|
||||
@parser = @parser
|
||||
@string_input = 'class.new(self.get(4))'
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:name=>"class"}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:receiver=>{:name=>"self"}, :call_site=>{:name=>"get"}, :argument_list=>[{:argument=>{:integer=>"4"}}]}}]}]}
|
||||
@transform_output = s(:list, [s(:call, s(:name, "new"), [s(:call, s(:name, "get"), [s(:int, 4)], s(:name, "self"))], s(:name, "class"))])
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -3,7 +3,7 @@ require_relative "../parser_helper"
|
||||
class RootTestClassDef < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_simplest_class
|
||||
@string_input = <<HERE
|
||||
class Foo
|
||||
@ -11,20 +11,20 @@ class Foo
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Foo", :derived_name=>nil, :class_expressions=>[{:integer=>"5"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ClassExpression.new(:Foo ,nil, [Ast::IntegerExpression.new(5)] )])
|
||||
@transform_output = s(:list, [s(:class, "Foo", nil, [s(:int, 5)])])
|
||||
end
|
||||
|
||||
def test_class_ops
|
||||
@string_input = <<HERE
|
||||
class Opers
|
||||
def foo(x)
|
||||
@abba = 5
|
||||
abba = 5
|
||||
2 + 5
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Opers", :derived_name=>nil, :class_expressions=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:l=>{:instance_variable=>{:name=>"abba"}}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ClassExpression.new(:Opers ,nil, [Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new(:x)] , [Ast::AssignmentExpression.new(Ast::VariableExpression.new(:abba),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] ,nil )] )])
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Opers", :derived_name=>nil, :class_expressions=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}], :end=>"end"}]}
|
||||
@transform_output = s(:list, [s(:class, "Opers", nil, [s(:function, s(:name, "foo"), [s(:name, "x")], [s(:assign, s(:name, "abba"), s(:int, 5)), s(:operator, "+", s(:int, 2), s(:int, 5))])])])
|
||||
end
|
||||
|
||||
def test_class_if
|
||||
@ -40,7 +40,7 @@ class Ifi
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Ifi", :derived_name=>nil, :class_expressions=>[{:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{: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::ExpressionList.new( [Ast::ClassExpression.new(:Ifi ,nil, [Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new(:n)] , [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:isit),Ast::IntegerExpression.new(42))],[Ast::AssignmentExpression.new(Ast::NameExpression.new(:maybenot),Ast::IntegerExpression.new(667))] )] ,nil )] )])
|
||||
@transform_output = s(:list, [s(:class, "Ifi", nil, [s(:function, s(:name, "ofthen"), [s(:name, "n")], [s(:if, s(:int, 0), [s(:assign, s(:name, "isit"), s(:int, 42))], [s(:assign, s(:name, "maybenot"), s(:int, 667))])])])])
|
||||
end
|
||||
|
||||
def test_class_function
|
||||
@ -53,7 +53,7 @@ class Pifi
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Pifi", :derived_name=>nil, :class_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}, {:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{:name=>"n"}}, {:parameter=>{:name=>"m"}}], :expressions=>[{:integer=>"44"}], :end=>"end"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ClassExpression.new(:Pifi ,nil, [Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ,Ast::NameExpression.new(:self)), Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new(:n),Ast::NameExpression.new(:m)] , [Ast::IntegerExpression.new(44)] ,nil )] )])
|
||||
@transform_output = s(:list, [s(:class, "Pifi", nil, [s(:call, s(:name, "ofthen"), [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:name, "var")]), s(:function, s(:name, "ofthen"), [s(:name, "n"), s(:name, "m")], [s(:int, 44)])])])
|
||||
end
|
||||
def test_class_module
|
||||
@string_input = <<HERE
|
||||
@ -64,7 +64,7 @@ class Foo
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Foo", :derived_name=>nil, :class_expressions=>[{:module_name=>"Boo", :module_expressions=>[{:call_site=>{:name=>"funcall"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}], :end=>"end"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ClassExpression.new(:Foo ,nil, [Ast::ModuleExpression.new(:Boo ,[Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ,Ast::NameExpression.new(:self))] )] )])
|
||||
@transform_output = s(:list, [s(:class, "Foo", nil, [s(:module, "Boo", [s(:call, s(:name, "funcall"), [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:name, "var")])])])])
|
||||
end
|
||||
def test_class_derived
|
||||
@string_input = <<HERE
|
||||
@ -73,7 +73,7 @@ class Foo < Object
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Foo", :derived_name=>{:module_name=>"Object"}, :class_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ClassExpression.new(:Foo ,:Object, [Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ,Ast::NameExpression.new(:self))] )])
|
||||
@transform_output = s(:list, [s(:class, "Foo", s(:module, "Object"), [s(:call, s(:name, "ofthen"), [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:name, "var")])])])
|
||||
end
|
||||
def test_class_method
|
||||
@string_input = <<HERE
|
||||
@ -84,7 +84,7 @@ class Foo < Object
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Foo", :derived_name=>{:module_name=>"Object"}, :class_expressions=>[{:receiver=>{:module_name=>"Foo"}, :function_name=>{:name=>"test"}, :parameter_list=>[], :expressions=>[{:integer=>"43"}], :end=>"end"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ClassExpression.new(:Foo ,:Object, [Ast::FunctionExpression.new(:test, [] , [Ast::IntegerExpression.new(43)] ,Ast::ModuleName.new(:Foo) )] )])
|
||||
@transform_output = s(:list, [s(:class, "Foo", s(:module, "Object"), [s(:function, s(:name, "test"), [], [s(:int, 43)], s(:module, "Foo"))])])
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -3,41 +3,41 @@ require_relative "../parser_helper"
|
||||
class RootTestCompound < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_one_array
|
||||
@string_input = '[42]'
|
||||
@parse_output = {:expression_list=>[{:array_constant=>[{:array_element=>{:integer=>"42"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ArrayExpression.new([Ast::IntegerExpression.new(42)])])
|
||||
@transform_output = s(:list, [s(:array, [s(:int, 42)])])
|
||||
end
|
||||
|
||||
def test_array_list
|
||||
@string_input = '[42, foo]'
|
||||
@parse_output = {:expression_list=>[{:array_constant=>[{:array_element=>{:integer=>"42"}}, {:array_element=>{:name=>"foo"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ArrayExpression.new([Ast::IntegerExpression.new(42), Ast::NameExpression.new(:foo)])])
|
||||
@transform_output = s(:list, [s(:array, [s(:int, 42), s(:name, "foo")])])
|
||||
end
|
||||
|
||||
def test_array_ops
|
||||
@string_input = '[ 3 + 4 , foo(22) ]'
|
||||
@parse_output = {:expression_list=>[{:array_constant=>[{:array_element=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"4"}}}, {:array_element=>{:call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"22"}}]}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ArrayExpression.new([Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)), Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(22)] ,Ast::NameExpression.new(:self))])])
|
||||
@transform_output = s(:list, [s(:array, [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:call, s(:name, "foo"), [s(:int, 22)])])])
|
||||
end
|
||||
|
||||
def test_hash
|
||||
@string_input = '{ foo => 33 }'
|
||||
@parse_output = {:expression_list=>[{:hash_constant=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:integer=>"33"}}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new(:foo) , Ast::IntegerExpression.new(33))])])
|
||||
@transform_output = s(:list, [s(:hash, [s(:assoc, s(:name, "foo"), s(:int, 33))])])
|
||||
end
|
||||
|
||||
def test_hash2
|
||||
@string_input = '{ foo => true }'
|
||||
@parse_output = {:expression_list=>[{:hash_constant=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:true=>"true"}}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new(:foo) , Ast::TrueExpression.new())])])
|
||||
@transform_output = s(:list, [s(:hash, [s(:assoc, s(:name, "foo"), s(:true))])])
|
||||
end
|
||||
|
||||
def test_hash_list
|
||||
@string_input = "{foo => 33 , bar => 42}"
|
||||
@parse_output = {:expression_list=>[{:hash_constant=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:integer=>"33"}}}, {:hash_pair=>{:hash_key=>{:name=>"bar"}, :hash_value=>{:integer=>"42"}}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new(:foo) , Ast::IntegerExpression.new(33)), Ast::AssociationExpression.new(Ast::NameExpression.new(:bar) , Ast::IntegerExpression.new(42))])])
|
||||
@transform_output = s(:list, [s(:hash, [s(:assoc, s(:name, "foo"), s(:int, 33)), s(:assoc, s(:name, "bar"), s(:int, 42))])])
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -13,7 +13,7 @@ else
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:if=>"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::IntegerExpression.new(42)],[Ast::IntegerExpression.new(667)] )])
|
||||
@transform_output = s(:list, [s(:if, s(:int, 0), [s(:int, 42)], [s(:int, 667)])])
|
||||
@parser = @parser
|
||||
end
|
||||
|
||||
@ -27,44 +27,29 @@ end
|
||||
HERE
|
||||
@string_input
|
||||
@parse_output = {:expression_list=>[{:if=>"if", :conditional=>{:l=>{:integer=>"3"}, :o=>"> ", :r=>{:name=>"var"}}, :if_true=>{:expressions=>[{:receiver=>{:module_name=>"Object"}, :call_site=>{:name=>"initialize"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}], :else=>"else"}, :if_false=>{:expressions=>[{:receiver=>{:name=>"var"}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:integer=>"33"}}]}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::IfExpression.new(Ast::OperatorExpression.new(">", Ast::IntegerExpression.new(3),Ast::NameExpression.new(:var)), [Ast::CallSiteExpression.new(:initialize, [Ast::IntegerExpression.new(3)] ,Ast::ModuleName.new(:Object))],[Ast::CallSiteExpression.new(:new, [Ast::IntegerExpression.new(33)] ,Ast::NameExpression.new(:var))] )])
|
||||
@transform_output = s(:list, [s(:if, s(:operator, ">", s(:int, 3), s(:name, "var")), [s(:call, s(:name, "initialize"), [s(:int, 3)], s(:module, "Object"))], [s(:call, s(:name, "new"), [s(:int, 33)], s(:name, "var"))])])
|
||||
end
|
||||
|
||||
def pest_if_end
|
||||
def test_if_end
|
||||
@string_input = <<HERE
|
||||
if(0)
|
||||
42
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:if=>"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::IntegerExpression.new(42)],[Ast::IntegerExpression.new(667)] )])
|
||||
@parse_output = {:expression_list=>[{:if=>"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:integer=>"42"}], :end=>"end"}}]}
|
||||
@transform_output = s(:list, [s(:if, s(:int, 0), [s(:int, 42)], nil)])
|
||||
@parser = @parser
|
||||
end
|
||||
|
||||
def pest_if_end_expressions
|
||||
def test_if_end_expressions
|
||||
@string_input = <<HERE
|
||||
if(3 > var)
|
||||
Object.initialize(3)
|
||||
end
|
||||
HERE
|
||||
@string_input
|
||||
@parse_output = {:expression_list=>[{:if=>"if", :conditional=>{:l=>{:integer=>"3"}, :o=>"> ", :r=>{:name=>"var"}}, :if_true=>{:expressions=>[{:receiver=>{:module_name=>"Object"}, :call_site=>{:name=>"initialize"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}], :else=>"else"}, :if_false=>{:expressions=>[{:receiver=>{:name=>"var"}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:integer=>"33"}}]}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::IfExpression.new(Ast::OperatorExpression.new(">", Ast::IntegerExpression.new(3),Ast::NameExpression.new(:var)), [Ast::CallSiteExpression.new(:initialize, [Ast::IntegerExpression.new(3)] ,Ast::ModuleName.new(:Object))],[Ast::CallSiteExpression.new(:new, [Ast::IntegerExpression.new(33)] ,Ast::NameExpression.new(:var))] )])
|
||||
@parse_output = {:expression_list=>[{:if=>"if", :conditional=>{:l=>{:integer=>"3"}, :o=>"> ", :r=>{:name=>"var"}}, :if_true=>{:expressions=>[{:receiver=>{:module_name=>"Object"}, :call_site=>{:name=>"initialize"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}], :end=>"end"}}]}
|
||||
@transform_output = s(:list, [s(:if, s(:operator, ">", s(:int, 3), s(:name, "var")), [s(:call, s(:name, "initialize"), [s(:int, 3)], s(:module, "Object"))], nil)])
|
||||
end
|
||||
|
||||
def pest_if_reverse
|
||||
@string_input = "42 if(0)"
|
||||
@parse_output = {:expression_list=>[{:if=>"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::IntegerExpression.new(42)],[Ast::IntegerExpression.new(667)] )])
|
||||
@parser = @parser
|
||||
end
|
||||
|
||||
def pest_if_end_expressions
|
||||
@string_input = "Object.initialize(3) if(3 > var)"
|
||||
@string_input
|
||||
@parse_output = {:expression_list=>[{:if=>"if", :conditional=>{:l=>{:integer=>"3"}, :o=>"> ", :r=>{:name=>"var"}}, :if_true=>{:expressions=>[{:receiver=>{:module_name=>"Object"}, :call_site=>{:name=>"initialize"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}], :else=>"else"}, :if_false=>{:expressions=>[{:receiver=>{:name=>"var"}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:integer=>"33"}}]}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::IfExpression.new(Ast::OperatorExpression.new(">", Ast::IntegerExpression.new(3),Ast::NameExpression.new(:var)), [Ast::CallSiteExpression.new(:initialize, [Ast::IntegerExpression.new(3)] ,Ast::ModuleName.new(:Object))],[Ast::CallSiteExpression.new(:new, [Ast::IntegerExpression.new(33)] ,Ast::NameExpression.new(:var))] )])
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
@ -11,53 +11,37 @@ def foo(x)
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:integer=>"5"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new(:x)] , [Ast::IntegerExpression.new(5)] ,nil )])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:function, s(:name, "foo"), [s(:name, "x")], [s(:int, 5)])])
|
||||
end
|
||||
|
||||
def test_function_no_braces
|
||||
def test_function_no_arg
|
||||
@string_input = <<HERE
|
||||
def foo
|
||||
def foo()
|
||||
5
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"foo"}, :expressions=>[{:integer=>"5"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:foo, [] , [Ast::IntegerExpression.new(5)] ,nil )])
|
||||
@parser = @parser
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[], :expressions=>[{:integer=>"5"}], :end=>"end"}]}
|
||||
@transform_output = s(:list, [s(:function, s(:name, "foo"), [], [s(:int, 5)])])
|
||||
end
|
||||
|
||||
def pest_function_no_braces_and_args
|
||||
def test_function_two_args
|
||||
@string_input = <<HERE
|
||||
def foo n , m
|
||||
def foo( n , m)
|
||||
n
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"foo"}, :expressions=>[{:name=>"n"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:foo, [] , [Ast::NameExpression.new(:n)] ,nil )])
|
||||
@parser = @parser
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"n"}}, {:parameter=>{:name=>"m"}}], :expressions=>[{:name=>"n"}], :end=>"end"}]}
|
||||
@transform_output = s(:list, [s(:function, s(:name, "foo"), [s(:name, "n"), s(:name, "m")], [s(:name, "n")])])
|
||||
end
|
||||
|
||||
def test_class_function
|
||||
@string_input = <<HERE
|
||||
def String.length(x)
|
||||
@length
|
||||
length
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:module_name=>"String"}, :function_name=>{:name=>"length"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:instance_variable=>{:name=>"length"}}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:length, [Ast::NameExpression.new(:x)] , [Ast::VariableExpression.new(:length)] ,Ast::ModuleName.new(:String) )])
|
||||
@parser = @parser
|
||||
end
|
||||
|
||||
def pest_function_default_args
|
||||
@string_input = <<HERE
|
||||
def foo(x = true )
|
||||
abba = 5
|
||||
2 + 5
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new(:x)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:abba),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] ,nil )])
|
||||
@parser = @parser
|
||||
@parse_output = {:expression_list=>[{:receiver=>{:module_name=>"String"}, :function_name=>{:name=>"length"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:name=>"length"}], :end=>"end"}]}
|
||||
@transform_output = s(:list, [s(:function, s(:name, "length"), [s(:name, "x")], [s(:name, "length")], s(:module, "String"))])
|
||||
end
|
||||
|
||||
def test_function_ops
|
||||
@ -68,8 +52,7 @@ def foo(x)
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new(:x)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:abba),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] ,nil )])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:function, s(:name, "foo"), [s(:name, "x")], [s(:assign, s(:name, "abba"), s(:int, 5)), s(:operator, "+", s(:int, 2), s(:int, 5))])])
|
||||
end
|
||||
|
||||
def test_function_if
|
||||
@ -83,8 +66,7 @@ def ofthen(n)
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{: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"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new(:n)] , [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:isit),Ast::IntegerExpression.new(42))],[Ast::AssignmentExpression.new(Ast::NameExpression.new(:maybenot),Ast::IntegerExpression.new(667))] )] ,nil )])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:function, s(:name, "ofthen"), [s(:name, "n")], [s(:if, s(:int, 0), [s(:assign, s(:name, "isit"), s(:int, 42))], [s(:assign, s(:name, "maybenot"), s(:int, 667))])])])
|
||||
end
|
||||
|
||||
def test_function_return
|
||||
@ -95,8 +77,7 @@ def retvar(n)
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"i"}, :o=>"= ", :r=>{:integer=>"5"}}, {:return=>"return", :return_expression=>{:name=>"i"}}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:i),Ast::IntegerExpression.new(5)),Ast::ReturnExpression.new(Ast::NameExpression.new(:i) )] ,nil )])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:function, s(:name, "retvar"), [s(:name, "n")], [s(:assign, s(:name, "i"), s(:int, 5)), s(:return, s(:name, "i"))])])
|
||||
end
|
||||
|
||||
def test_function_return_if
|
||||
@ -110,37 +91,34 @@ def retvar(n)
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:if=>"if", :conditional=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"5"}}, :if_true=>{:expressions=>[{:return=>"return", :return_expression=>{:integer=>"10"}}], :else=>"else"}, :if_false=>{:expressions=>[{:return=>"return", :return_expression=>{:integer=>"20"}}], :end=>"end"}}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new(:n)] , [Ast::IfExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(5)), [Ast::ReturnExpression.new(Ast::IntegerExpression.new(10) )],[Ast::ReturnExpression.new(Ast::IntegerExpression.new(20) )] )] ,nil )])
|
||||
@parser = @parser
|
||||
@transform_output = s(:list, [s(:function, s(:name, "retvar"), [s(:name, "n")], [s(:if, s(:operator, ">", s(:name, "n"), s(:int, 5)), [s(:return, s(:int, 10))], [s(:return, s(:int, 20))])])])
|
||||
end
|
||||
|
||||
def test_function_return_while
|
||||
@string_input = <<HERE
|
||||
def retvar(n)
|
||||
while( n > 5) do
|
||||
while( n > 5)
|
||||
n = n + 1
|
||||
return n
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"5"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"+ ", :r=>{:integer=>"1"}}}, {:return=>"return", :return_expression=>{:name=>"n"}}], :end=>"end"}}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new(:n)] , [Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(5)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1))), Ast::ReturnExpression.new(Ast::NameExpression.new(:n) )] )] ,nil )])
|
||||
@parser = @parser
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"5"}}, :body=>{:expressions=>[{:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"+ ", :r=>{:integer=>"1"}}}, {:return=>"return", :return_expression=>{:name=>"n"}}], :end=>"end"}}], :end=>"end"}]}
|
||||
@transform_output = s(:list, [s(:function, s(:name, "retvar"), [s(:name, "n")], [s(:while, s(:operator, ">", s(:name, "n"), s(:int, 5)), [s(:assign, s(:name, "n"), s(:operator, "+", s(:name, "n"), s(:int, 1))), s(:return, s(:name, "n"))])])])
|
||||
end
|
||||
|
||||
def test_function_while
|
||||
@string_input = <<HERE
|
||||
def fibonaccit(n)
|
||||
a = 0
|
||||
while (n) do
|
||||
while(n)
|
||||
some = 43
|
||||
other = some * 4
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:while=>"while", :while_cond=>{:name=>"n"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"some"}, :o=>"= ", :r=>{:integer=>"43"}}, {:l=>{:name=>"other"}, :o=>"= ", :r=>{:l=>{:name=>"some"}, :o=>"* ", :r=>{:integer=>"4"}}}], :end=>"end"}}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(0)),Ast::WhileExpression.new(Ast::NameExpression.new(:n), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:some),Ast::IntegerExpression.new(43)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:other),Ast::OperatorExpression.new("*", Ast::NameExpression.new(:some),Ast::IntegerExpression.new(4)))] )] ,nil )])
|
||||
@parser = @parser
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:while=>"while", :while_cond=>{:name=>"n"}, :body=>{:expressions=>[{:l=>{:name=>"some"}, :o=>"= ", :r=>{:integer=>"43"}}, {:l=>{:name=>"other"}, :o=>"= ", :r=>{:l=>{:name=>"some"}, :o=>"* ", :r=>{:integer=>"4"}}}], :end=>"end"}}], :end=>"end"}]}
|
||||
@transform_output = s(:list, [s(:function, s(:name, "fibonaccit"), [s(:name, "n")], [s(:assign, s(:name, "a"), s(:int, 0)), s(:while, s(:name, "n"), [s(:assign, s(:name, "some"), s(:int, 43)), s(:assign, s(:name, "other"), s(:operator, "*", s(:name, "some"), s(:int, 4)))])])])
|
||||
end
|
||||
|
||||
def test_function_big_while
|
||||
@ -148,7 +126,7 @@ HERE
|
||||
def fibonaccit(n)
|
||||
a = 0
|
||||
b = 1
|
||||
while( n > 1 ) do
|
||||
while( n > 1 )
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp + b
|
||||
@ -157,8 +135,7 @@ def fibonaccit(n)
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:integer=>"1"}}, {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(0)),Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::IntegerExpression.new(1)),Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::NameExpression.new(:b)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:b))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)))] )] ,nil )])
|
||||
@parser = @parser
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:integer=>"1"}}, {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}], :end=>"end"}]}
|
||||
@transform_output = s(:list, [s(:function, s(:name, "fibonaccit"), [s(:name, "n")], [s(:assign, s(:name, "a"), s(:int, 0)), s(:assign, s(:name, "b"), s(:int, 1)), s(:while, s(:operator, ">", s(:name, "n"), s(:int, 1)), [s(:assign, s(:name, "tmp"), s(:name, "a")), s(:assign, s(:name, "a"), s(:name, "b")), s(:assign, s(:name, "b"), s(:operator, "+", s(:name, "tmp"), s(:name, "b"))), s(:call, s(:name, "puts"), [s(:name, "b")]), s(:assign, s(:name, "n"), s(:operator, "-", s(:name, "n"), s(:int, 1)))])])])
|
||||
end
|
||||
end
|
||||
|
@ -3,7 +3,7 @@ require_relative "../parser_helper"
|
||||
class RootTestModuleDef < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_simplest_module
|
||||
@string_input = <<HERE
|
||||
module Simple
|
||||
@ -11,7 +11,7 @@ module Simple
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Simple", :module_expressions=>[{:integer=>"5"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ModuleExpression.new(:Simple ,[Ast::IntegerExpression.new(5)] )])
|
||||
@transform_output = s(:list, [s(:module, "Simple", [s(:int, 5)])])
|
||||
end
|
||||
|
||||
def test_module_ops
|
||||
@ -24,17 +24,17 @@ module Opers
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Opers", :module_expressions=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ModuleExpression.new(:Opers ,[Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new(:x)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:abba),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] ,nil )] )])
|
||||
@transform_output = s(:list, [s(:module, "Opers", [s(:function, s(:name, "foo"), [s(:name, "x")], [s(:assign, s(:name, "abba"), s(:int, 5)), s(:operator, "+", s(:int, 2), s(:int, 5))])])])
|
||||
end
|
||||
|
||||
def test_module_assign_instance
|
||||
def test_module_assign_var
|
||||
@string_input = <<HERE
|
||||
module Opers
|
||||
@abba = 5
|
||||
abba = 5
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Opers", :module_expressions=>[{:l=>{:instance_variable=>{:name=>"abba"}}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ModuleExpression.new(:Opers ,[Ast::AssignmentExpression.new(Ast::VariableExpression.new(:abba),Ast::IntegerExpression.new(5))] )])
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Opers", :module_expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}]}
|
||||
@transform_output = s(:list, [s(:module, "Opers", [s(:assign, s(:name, "abba"), s(:int, 5))])])
|
||||
end
|
||||
|
||||
def test_module_if
|
||||
@ -50,7 +50,7 @@ module Foo
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Foo", :module_expressions=>[{:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{: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::ExpressionList.new( [Ast::ModuleExpression.new(:Foo ,[Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new(:n)] , [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:isit),Ast::IntegerExpression.new(42))],[Ast::AssignmentExpression.new(Ast::NameExpression.new(:maybenot),Ast::IntegerExpression.new(667))] )] ,nil )] )])
|
||||
@transform_output = s(:list, [s(:module, "Foo", [s(:function, s(:name, "ofthen"), [s(:name, "n")], [s(:if, s(:int, 0), [s(:assign, s(:name, "isit"), s(:int, 42))], [s(:assign, s(:name, "maybenot"), s(:int, 667))])])])])
|
||||
end
|
||||
|
||||
def test_module_function
|
||||
@ -63,7 +63,7 @@ module Soho
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Soho", :module_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}, {:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{:name=>"n"}}, {:parameter=>{:name=>"m"}}], :expressions=>[{:integer=>"44"}], :end=>"end"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ModuleExpression.new(:Soho ,[Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ,Ast::NameExpression.new(:self)), Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new(:n),Ast::NameExpression.new(:m)] , [Ast::IntegerExpression.new(44)] ,nil )] )])
|
||||
@transform_output = s(:list, [s(:module, "Soho", [s(:call, s(:name, "ofthen"), [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:name, "var")]), s(:function, s(:name, "ofthen"), [s(:name, "n"), s(:name, "m")], [s(:int, 44)])])])
|
||||
end
|
||||
|
||||
def test_function_without_braces
|
||||
@ -75,7 +75,7 @@ class Foo
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Foo", :derived_name=>nil, :class_expressions=>[{:function_name=>{:name=>"bar"}, :expressions=>[{:integer=>"4"}], :end=>"end"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ClassExpression.new(:Foo ,nil, [Ast::FunctionExpression.new(:bar, [] , [Ast::IntegerExpression.new(4)] ,nil )] )])
|
||||
@transform_output = s(:list, [s(:class, "Foo", nil, [s(:function, s(:name, "bar"), [], [s(:int, 4)])])])
|
||||
end
|
||||
|
||||
def test_module_class
|
||||
@ -87,6 +87,6 @@ module Foo
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Foo", :module_expressions=>[{:module_name=>"Bar", :derived_name=>nil, :class_expressions=>[{:call_site=>{:name=>"funcall"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}], :end=>"end"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ModuleExpression.new(:Foo ,[Ast::ClassExpression.new(:Bar ,nil, [Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ,Ast::NameExpression.new(:self))] )] )])
|
||||
@transform_output = s(:list, [s(:module, "Foo", [s(:class, "Bar", nil, [s(:call, s(:name, "funcall"), [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:name, "var")])])])])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -7,7 +7,7 @@ class RootTestExpressions < MiniTest::Test
|
||||
def simple_op op
|
||||
@string_input = "5 #{op} 3"
|
||||
@parse_output = {:expression_list=>[{:l=>{:integer=>"5"}, :o=>"#{op} ", :r=>{:integer=>"3"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new(op, Ast::IntegerExpression.new(5),Ast::IntegerExpression.new(3))])
|
||||
@transform_output = s(:list, [s(:operator, op, s(:int, 5), s(:int, 3))])
|
||||
end
|
||||
def test_simple_multiply
|
||||
simple_op "*"
|
||||
@ -27,55 +27,50 @@ class RootTestExpressions < MiniTest::Test
|
||||
def test_simple_smaller
|
||||
simple_op "<"
|
||||
end
|
||||
def test_op_variable
|
||||
@string_input = "a + 35"
|
||||
@parse_output = {:expression_list=>[{:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("+", Ast::NameExpression.new(:a),Ast::IntegerExpression.new(35))])
|
||||
end
|
||||
def test_op_two_variable
|
||||
@string_input = "a - b"
|
||||
@parse_output = {:expression_list=>[{:l=>{:name=>"a"}, :o=>"- ", :r=>{:name=>"b"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::NameExpression.new(:b))])
|
||||
@transform_output = s(:list, [s(:operator, "-", s(:name, "a"), s(:name, "b"))])
|
||||
end
|
||||
def test_op_instance_variable
|
||||
@string_input = "@a - 5"
|
||||
@parse_output = {:expression_list=>[{:l=>{:instance_variable=>{:name=>"a"}}, :o=>"- ", :r=>{:integer=>"5"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("-", Ast::VariableExpression.new(:a),Ast::IntegerExpression.new(5))])
|
||||
def test_op_variable
|
||||
@string_input = "a - 5"
|
||||
@parse_output = {:expression_list=>[{:l=>{:name=>"a"}, :o=>"- ", :r=>{:integer=>"5"}}]}
|
||||
@transform_output = s(:list, [s(:operator, "-", s(:name, "a"), s(:int, 5))])
|
||||
end
|
||||
def test_op_variable_string
|
||||
@string_input = 'a - "st"'
|
||||
@parse_output = {:expression_list=>[{:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::StringExpression.new("st"))])
|
||||
@transform_output = s(:list, [s(:operator, "-", s(:name, "a"), s(:string, "st"))])
|
||||
end
|
||||
def test_op_variable_true
|
||||
@string_input = 'a == true'
|
||||
@parse_output = {:expression_list=>[{:l=>{:name=>"a"}, :o=>"== ", :r=>{:true=>"true"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("==", Ast::NameExpression.new(:a),Ast::TrueExpression.new())])
|
||||
@transform_output = s(:list, [s(:operator, "==", s(:name, "a"), s(:true))])
|
||||
end
|
||||
def test_two_same_ops
|
||||
@string_input = '2 + 3 + 4'
|
||||
@parse_output = {:expression_list=>[{:l=>{:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4))])
|
||||
@transform_output = s(:list, [s(:operator, "+", s(:operator, "+", s(:int, 2), s(:int, 3)), s(:int, 4))])
|
||||
end
|
||||
def test_two_different_ops
|
||||
@string_input = '2 + 3 * 4'
|
||||
@parse_output = {:expression_list=>[{:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:l=>{:integer=>"3"}, :o=>"* ", :r=>{:integer=>"4"}}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)))])
|
||||
@transform_output = s(:list, [s(:operator, "+", s(:int, 2), s(:operator, "*", s(:int, 3), s(:int, 4)))])
|
||||
end
|
||||
def test_two_different_ops_order
|
||||
@string_input = '2 * 3 + 4'
|
||||
@parse_output = {:expression_list=>[{:l=>{:l=>{:integer=>"2"}, :o=>"* ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4))])
|
||||
@transform_output = s(:list, [s(:operator, "+", s(:operator, "*", s(:int, 2), s(:int, 3)), s(:int, 4))])
|
||||
end
|
||||
def test_assignment
|
||||
@string_input = "a = 5"
|
||||
@parse_output = {:expression_list=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(5))])
|
||||
@transform_output = s(:list, [s(:assign, s(:name, "a"), s(:int, 5))])
|
||||
end
|
||||
def test_assignment_instance
|
||||
@string_input = "@a = 5"
|
||||
@parse_output = {:expression_list=>[{:l=>{:instance_variable=>{:name=>"a"}}, :o=>"= ", :r=>{:integer=>"5"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::AssignmentExpression.new(Ast::VariableExpression.new(:a),Ast::IntegerExpression.new(5))])
|
||||
@string_input = "a = 5"
|
||||
@parse_output = {:expression_list=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}]}
|
||||
@transform_output = s(:list, [s(:assign, s(:name, "a"), s(:int, 5))])
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -3,23 +3,23 @@ require_relative "../parser_helper"
|
||||
class RootTestReturn < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_return_int
|
||||
@string_input = 'return 42'
|
||||
@parse_output = {:expression_list=>[{:name=>"return"}, {:integer=>"42"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::NameExpression.new(:return),Ast::IntegerExpression.new(42)])
|
||||
@parse_output = {:expression_list=>[{:return=>"return", :return_expression=>{:integer=>"42"}}]}
|
||||
@transform_output = s(:list, [s(:return, s(:int, 42))])
|
||||
end
|
||||
|
||||
def test_return_variable
|
||||
@string_input = 'return foo'
|
||||
@parse_output = {:expression_list=>[{:name=>"return"}, {:name=>"foo"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::NameExpression.new(:return),Ast::NameExpression.new(:foo)])
|
||||
@parse_output = {:expression_list=>[{:return=>"return", :return_expression=>{:name=>"foo"}}]}
|
||||
@transform_output = s(:list, [s(:return, s(:name, "foo"))])
|
||||
end
|
||||
|
||||
def test_return_string
|
||||
@string_input = 'return "hello"'
|
||||
@parse_output = {:expression_list=>[{:name=>"return"}, {:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::NameExpression.new(:return),Ast::StringExpression.new("hello")])
|
||||
@parse_output = {:expression_list=>[{:return=>"return", :return_expression=>{:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}}]}
|
||||
@transform_output = s(:list, [s(:return, s(:string, "hello"))])
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -3,30 +3,30 @@ require_relative "../parser_helper"
|
||||
class RootTestRoot < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_double_root
|
||||
@string_input = <<HERE
|
||||
def foo(x)
|
||||
def foo(x)
|
||||
a = 5
|
||||
end
|
||||
|
||||
foo( 3 )
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}, {:call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new(:x)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(5))] ,nil ),Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(3)] ,Ast::NameExpression.new(:self))])
|
||||
@transform_output = s(:list, [s(:function, s(:name, "foo"), [s(:name, "x")], [s(:assign, s(:name, "a"), s(:int, 5))]), s(:call, s(:name, "foo"), [s(:int, 3)])])
|
||||
end
|
||||
|
||||
def test_comments
|
||||
def ttest_comments
|
||||
@string_input = <<HERE
|
||||
def foo(x) #here
|
||||
a = 0 # a == r1
|
||||
b = 1 # b = r2
|
||||
while(n<1) do #comment
|
||||
while(n<1) #comment
|
||||
tmp = a # r3 <- r1
|
||||
a = b # r1 <- r2
|
||||
b = tmp + b # r4 = r2 + r3 (r4 transient) r2 <- r4
|
||||
putstring(b)
|
||||
n = n - 1 #me
|
||||
b = tmp + b # r4 = r2 + r3 (r4 transient) r2 <- r4
|
||||
putstring(b)
|
||||
n = n - 1 #me
|
||||
end #no
|
||||
end #anywhere
|
||||
foo( 3 ) #and more
|
||||
@ -38,9 +38,9 @@ HERE
|
||||
def test_fibo1
|
||||
@string_input = <<HERE
|
||||
def fibonaccit(n)
|
||||
a = 0
|
||||
a = 0
|
||||
b = 1
|
||||
while( n > 1 ) do
|
||||
while( n > 1 )
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp + b
|
||||
@ -51,15 +51,15 @@ end
|
||||
|
||||
fibonaccit( 10 )
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:integer=>"1"}}, {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}], :end=>"end"}, {:call_site=>{:name=>"fibonaccit"}, :argument_list=>[{:argument=>{:integer=>"10"}}]}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(0)),Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::IntegerExpression.new(1)),Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::NameExpression.new(:b)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:b))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)))] )] ,nil ),Ast::CallSiteExpression.new(:fibonaccit, [Ast::IntegerExpression.new(10)] ,Ast::NameExpression.new(:self))])
|
||||
@parse_output = {:expression_list=>[{:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:integer=>"1"}}, {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}], :end=>"end"}, {:call_site=>{:name=>"fibonaccit"}, :argument_list=>[{:argument=>{:integer=>"10"}}]}]}
|
||||
@transform_output = s(:list, [s(:function, s(:name, "fibonaccit"), [s(:name, "n")], [s(:assign, s(:name, "a"), s(:int, 0)), s(:assign, s(:name, "b"), s(:int, 1)), s(:while, s(:operator, ">", s(:name, "n"), s(:int, 1)), [s(:assign, s(:name, "tmp"), s(:name, "a")), s(:assign, s(:name, "a"), s(:name, "b")), s(:assign, s(:name, "b"), s(:operator, "+", s(:name, "tmp"), s(:name, "b"))), s(:call, s(:name, "puts"), [s(:name, "b")]), s(:assign, s(:name, "n"), s(:operator, "-", s(:name, "n"), s(:int, 1)))])]), s(:call, s(:name, "fibonaccit"), [s(:int, 10)])])
|
||||
end
|
||||
|
||||
|
||||
def test_module_method
|
||||
@string_input = <<HERE
|
||||
module Fibo
|
||||
def fibonaccit(n)
|
||||
a = 0
|
||||
a = 0
|
||||
end
|
||||
|
||||
fibonaccit( 10 )
|
||||
@ -67,7 +67,7 @@ end
|
||||
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Fibo", :module_expressions=>[{:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}], :end=>"end"}, {:call_site=>{:name=>"fibonaccit"}, :argument_list=>[{:argument=>{:integer=>"10"}}]}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ModuleExpression.new(:Fibo ,[Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(0))] ,nil ), Ast::CallSiteExpression.new(:fibonaccit, [Ast::IntegerExpression.new(10)] ,Ast::NameExpression.new(:self))] )])
|
||||
@transform_output = s(:list, [s(:module, "Fibo", [s(:function, s(:name, "fibonaccit"), [s(:name, "n")], [s(:assign, s(:name, "a"), s(:int, 0))]), s(:call, s(:name, "fibonaccit"), [s(:int, 10)])])])
|
||||
end
|
||||
|
||||
def test_module_assignment
|
||||
@ -79,7 +79,7 @@ end
|
||||
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"Fibo", :module_expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:l=>{:integer=>"5"}, :o=>"+ ", :r=>{:name=>"foo"}}}, {:call_site=>{:name=>"bar"}, :argument_list=>[{:argument=>{:name=>"b"}}, {:argument=>{:name=>"a"}}, {:argument=>{:name=>"r"}}]}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ModuleExpression.new(:Fibo ,[Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(5),Ast::NameExpression.new(:foo))), Ast::CallSiteExpression.new(:bar, [Ast::NameExpression.new(:b),Ast::NameExpression.new(:a),Ast::NameExpression.new(:r)] ,Ast::NameExpression.new(:self))] )])
|
||||
@transform_output = s(:list, [s(:module, "Fibo", [s(:assign, s(:name, "a"), s(:operator, "+", s(:int, 5), s(:name, "foo"))), s(:call, s(:name, "bar"), [s(:name, "b"), s(:name, "a"), s(:name, "r")])])])
|
||||
end
|
||||
|
||||
def test_root_module_class
|
||||
@ -92,7 +92,7 @@ end
|
||||
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"FooBo", :module_expressions=>[{:module_name=>"Bar", :derived_name=>nil, :class_expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:l=>{:integer=>"5"}, :o=>"+ ", :r=>{:name=>"foo"}}}], :end=>"end"}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ModuleExpression.new(:FooBo ,[Ast::ClassExpression.new(:Bar ,nil, [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(5),Ast::NameExpression.new(:foo)))] )] )])
|
||||
@transform_output = s(:list, [s(:module, "FooBo", [s(:class, "Bar", nil, [s(:assign, s(:name, "a"), s(:operator, "+", s(:int, 5), s(:name, "foo")))])])])
|
||||
end
|
||||
|
||||
def test_class_method
|
||||
@ -103,9 +103,7 @@ end
|
||||
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:module_name=>"FooBo", :derived_name=>nil, :class_expressions=>[{:receiver=>{:module_name=>"Bar"}, :call_site=>{:name=>"call"}, :argument_list=>[{:argument=>{:integer=>"35"}}]}], :end=>"end"}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::ClassExpression.new(:FooBo ,nil, [Ast::CallSiteExpression.new(:call, [Ast::IntegerExpression.new(35)] ,Ast::ModuleName.new(:Bar))] )])
|
||||
@transform_output = s(:list, [s(:class, "FooBo", nil, [s(:call, s(:name, "call"), [s(:int, 35)], s(:module, "Bar"))])])
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
@ -6,38 +6,29 @@ class RootTestWhile < MiniTest::Test
|
||||
|
||||
def test_while
|
||||
@string_input = <<HERE
|
||||
while(1) do
|
||||
while(1)
|
||||
tmp = a
|
||||
puts(b)
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::WhileExpression.new(Ast::IntegerExpression.new(1), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self))] )])
|
||||
@parser = @parser
|
||||
end
|
||||
|
||||
def pest_while_reverse
|
||||
@string_input = "puts '1' while true "
|
||||
@parse_output = {:expression_list=>[{:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::WhileExpression.new(Ast::IntegerExpression.new(1), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self))] )])
|
||||
@parser = @parser
|
||||
@parse_output = {:expression_list=>[{:while=>"while", :while_cond=>{:integer=>"1"}, :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}], :end=>"end"}}]}
|
||||
@transform_output = s(:list, [s(:while, s(:int, 1), [s(:assign, s(:name, "tmp"), s(:name, "a")), s(:call, s(:name, "puts"), [s(:name, "b")])])])
|
||||
end
|
||||
|
||||
def test_while_method
|
||||
@string_input = <<HERE
|
||||
while(1) do
|
||||
while(1)
|
||||
tmp = String.new()
|
||||
tmp.puts(i)
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:receiver=>{:module_name=>"String"}, :call_site=>{:name=>"new"}, :argument_list=>[]}}, {:receiver=>{:name=>"tmp"}, :call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"i"}}]}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::WhileExpression.new(Ast::IntegerExpression.new(1), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::CallSiteExpression.new(:new, [] ,Ast::ModuleName.new(:String))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:i)] ,Ast::NameExpression.new(:tmp))] )])
|
||||
@parser = @parser
|
||||
@parse_output = {:expression_list=>[{:while=>"while", :while_cond=>{:integer=>"1"}, :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:receiver=>{:module_name=>"String"}, :call_site=>{:name=>"new"}, :argument_list=>[]}}, {:receiver=>{:name=>"tmp"}, :call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"i"}}]}], :end=>"end"}}]}
|
||||
@transform_output = s(:list, [s(:while, s(:int, 1), [s(:assign, s(:name, "tmp"), s(:call, s(:name, "new"), [], s(:module, "String"))), s(:call, s(:name, "puts"), [s(:name, "i")], s(:name, "tmp"))])])
|
||||
end
|
||||
|
||||
def test_big_while
|
||||
@string_input = <<HERE
|
||||
while( n > 1) do
|
||||
while( n > 1)
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp + b
|
||||
@ -45,8 +36,7 @@ while( n > 1) do
|
||||
n = n - 1
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:expression_list=>[{:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}]}
|
||||
@transform_output = Ast::ExpressionList.new( [Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::NameExpression.new(:b)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:b))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)))] )])
|
||||
@parser = @parser
|
||||
@parse_output = {:expression_list=>[{:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}]}
|
||||
@transform_output = s(:list, [s(:while, s(:operator, ">", s(:name, "n"), s(:int, 1)), [s(:assign, s(:name, "tmp"), s(:name, "a")), s(:assign, s(:name, "a"), s(:name, "b")), s(:assign, s(:name, "b"), s(:operator, "+", s(:name, "tmp"), s(:name, "b"))), s(:call, s(:name, "puts"), [s(:name, "b")]), s(:assign, s(:name, "n"), s(:operator, "-", s(:name, "n"), s(:int, 1)))])])
|
||||
end
|
||||
end
|
||||
|
@ -3,11 +3,11 @@ require_relative "../parser_helper"
|
||||
class TestArguments < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_one_argument
|
||||
@string_input = '(42)'
|
||||
@parse_output = {:argument_list => [{:argument => {:integer => '42'}}] }
|
||||
@transform_output = [Ast::IntegerExpression.new(42) ]
|
||||
@transform_output = [s(:int, 42)]
|
||||
@parser = @parser.argument_list
|
||||
end
|
||||
|
||||
@ -15,14 +15,14 @@ class TestArguments < MiniTest::Test
|
||||
@string_input = '(42, foo)'
|
||||
@parse_output = {:argument_list => [{:argument => {:integer => '42'}},
|
||||
{:argument => {:name => 'foo'}}]}
|
||||
@transform_output = [Ast::IntegerExpression.new(42), Ast::NameExpression.new('foo')]
|
||||
@transform_output = [s(:int, 42), s(:name, "foo")]
|
||||
@parser = @parser.argument_list
|
||||
end
|
||||
|
||||
def test_parameter
|
||||
@string_input = "(foo)"
|
||||
@parse_output = {:parameter_list=>[{:parameter=>{:name=>"foo"}}]}
|
||||
@transform_output = [Ast::NameExpression.new('foo')]
|
||||
@transform_output = [s(:name, "foo")]
|
||||
@parser = @parser.parameter_list
|
||||
end
|
||||
|
||||
@ -30,8 +30,8 @@ class TestArguments < MiniTest::Test
|
||||
@string_input = "( foo , bar)"
|
||||
@parse_output = {:parameter_list => [{:parameter => { :name => "foo"}},
|
||||
{:parameter => { :name => "bar"}} ]}
|
||||
@transform_output = [Ast::NameExpression.new('foo') , Ast::NameExpression.new('bar')]
|
||||
@transform_output = [s(:name, "foo"), s(:name, "bar")]
|
||||
@parser = @parser.parameter_list
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -5,63 +5,63 @@ class TestBasic < MiniTest::Test
|
||||
include ParserHelper
|
||||
|
||||
def test_true
|
||||
@string_input = 'true '
|
||||
@string_input = 'true'
|
||||
@parse_output = {:true => 'true'}
|
||||
@transform_output = Ast::TrueExpression.new()
|
||||
@transform_output = s(:true)
|
||||
@parser = @parser.basic_type
|
||||
end
|
||||
def test_false
|
||||
@string_input = 'false '
|
||||
@string_input = 'false'
|
||||
@parse_output = {:false => 'false'}
|
||||
@transform_output = Ast::FalseExpression.new()
|
||||
@transform_output = s(:false)
|
||||
@parser = @parser.basic_type
|
||||
end
|
||||
def test_nil
|
||||
@string_input = 'nil '
|
||||
@string_input = 'nil'
|
||||
@parse_output = {:nil => 'nil'}
|
||||
@transform_output = Ast::NilExpression.new()
|
||||
@transform_output = s(:nil)
|
||||
@parser = @parser.basic_type
|
||||
end
|
||||
|
||||
def test_number
|
||||
@string_input = '42 '
|
||||
@parse_output = {:integer => '42'}
|
||||
@transform_output = Ast::IntegerExpression.new(42)
|
||||
@transform_output = s(:int , 42)
|
||||
@parser = @parser.integer
|
||||
end
|
||||
|
||||
def test_name
|
||||
@string_input = 'foo '
|
||||
@parse_output = {:name => 'foo'}
|
||||
@transform_output = Ast::NameExpression.new('foo')
|
||||
@transform_output = s(:name, 'foo')
|
||||
@parser = @parser.name
|
||||
end
|
||||
|
||||
def test_name_underscode_start
|
||||
@string_input = '_bar '
|
||||
@parse_output = {:name => '_bar'}
|
||||
@transform_output = Ast::NameExpression.new('_bar')
|
||||
@transform_output = s(:name, '_bar')
|
||||
@parser = @parser.name
|
||||
end
|
||||
|
||||
def test_name_underscode_middle
|
||||
@string_input = 'foo_bar '
|
||||
@parse_output = {:name => 'foo_bar'}
|
||||
@transform_output = Ast::NameExpression.new('foo_bar')
|
||||
@transform_output = s(:name, 'foo_bar')
|
||||
@parser = @parser.name
|
||||
end
|
||||
|
||||
def test_instance_variable
|
||||
@string_input = '@foo_bar '
|
||||
@parse_output = {:instance_variable=>{:name=>"foo_bar"}}
|
||||
@transform_output = Ast::VariableExpression.new(:foo_bar)
|
||||
@parser = @parser.instance_variable
|
||||
end
|
||||
def test_field
|
||||
@string_input = 'int foo_bar '
|
||||
@parse_output = {:type=>"int", :name=>"foo_bar"}
|
||||
@transform_output = s(:field , :int , :foo_bar)
|
||||
@parser = @parser.field
|
||||
end
|
||||
|
||||
def test_module_name
|
||||
@string_input = 'FooBar '
|
||||
@parse_output = {:module_name=>"FooBar"}
|
||||
@transform_output = Ast::ModuleName.new("FooBar")
|
||||
@transform_output = s(:module , "FooBar")
|
||||
@parser = @parser.module_name
|
||||
end
|
||||
|
||||
@ -76,17 +76,17 @@ class TestBasic < MiniTest::Test
|
||||
def test_string
|
||||
@string_input = "\"hello\""
|
||||
@parse_output = {:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}
|
||||
@transform_output = Ast::StringExpression.new('hello')
|
||||
@transform_output = s(:string, "hello")
|
||||
@parser = @parser.string
|
||||
end
|
||||
|
||||
def test_string_escapes
|
||||
out = 'hello \nyou'
|
||||
@string_input = '"' + out + '"'
|
||||
@parse_output = {:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"},
|
||||
@parse_output = {:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"},
|
||||
{:char=>" "}, {:char=>" "}, {:esc=>"n"}, {:char=>"y"}, {:char=>"o"}, {:char=>"u"}]}
|
||||
@transform_output = Ast::StringExpression.new(out)
|
||||
@transform_output = s(:string, out)
|
||||
@parser = @parser.string
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -1,48 +0,0 @@
|
||||
require_relative "../parser_helper"
|
||||
|
||||
class TestBlock < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
def pest_block
|
||||
@string_input = <<HERE
|
||||
while(1) do
|
||||
tmp = a
|
||||
puts(b)
|
||||
end
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}], :end=>"end"}}
|
||||
@transform_output = Ast::BlockExpression.new(Ast::IntegerExpression.new(1), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self))] )
|
||||
@parser = @parser.block
|
||||
end
|
||||
|
||||
def pest_block_method
|
||||
@string_input = <<HERE
|
||||
while(1) do
|
||||
tmp = String.new()
|
||||
tmp.puts(i)
|
||||
end
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:receiver=>{:module_name=>"String"}, :call_site=>{:name=>"new"}, :argument_list=>[]}}, {:receiver=>{:name=>"tmp"}, :call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"i"}}]}], :end=>"end"}}
|
||||
@transform_output = Ast::BlockExpression.new(Ast::IntegerExpression.new(1), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::CallSiteExpression.new(:new, [] ,Ast::ModuleName.new(:String))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:i)] ,Ast::NameExpression.new(:tmp))] )
|
||||
@parser = @parser.block
|
||||
end
|
||||
|
||||
def pest_big_block
|
||||
@string_input = <<HERE
|
||||
while( n > 1) do
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp + b
|
||||
puts(b)
|
||||
n = n - 1
|
||||
end
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}
|
||||
@transform_output = Ast::BlockExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::NameExpression.new(:b)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:b))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)))] )
|
||||
@parser = @parser.block
|
||||
end
|
||||
end
|
@ -3,54 +3,48 @@ require_relative "../parser_helper"
|
||||
class TestCallSite < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_single_argument
|
||||
@string_input = 'foo(42)'
|
||||
@parse_output = {:call_site => {:name => 'foo'},
|
||||
:argument_list => [{:argument => {:integer => '42'} }] }
|
||||
@transform_output = Ast::CallSiteExpression.new :foo, [Ast::IntegerExpression.new(42)]
|
||||
@transform_output = s(:call, s(:name, "foo"), [s(:int, 42)])
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_single_self
|
||||
@string_input = 'self.foo(42)'
|
||||
@parse_output = {:receiver=>{:name=>"self"}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new :foo, [Ast::IntegerExpression.new(42)]
|
||||
@transform_output = s(:call, s(:name, "foo"), [s(:int, 42)], s(:name, "self"))
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_single_instance
|
||||
@string_input = '@var.foo(42)'
|
||||
@parse_output = {:receiver=>{:instance_variable=>{:name=>"var"}}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::VariableExpression.new(:var))
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_single_name
|
||||
@string_input = 'my_my.foo(42)'
|
||||
@parse_output = {:receiver=>{:name=>"my_my"}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::NameExpression.new("my_my"))
|
||||
@transform_output = s(:call, s(:name, "foo"), [s(:int, 42)], s(:name, "my_my"))
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_int_receiver
|
||||
@string_input = '42.put()'
|
||||
@parse_output = {:receiver=>{:integer=>"42"}, :call_site=>{:name=>"put"}, :argument_list=>[]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:put, [] ,Ast::IntegerExpression.new(42))
|
||||
@transform_output = s(:call, s(:name, "put"), [], s(:int, 42))
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_string_receiver
|
||||
@string_input = '"hello".puts()'
|
||||
@parse_output = {:receiver=>{:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}, :call_site=>{:name=>"puts"}, :argument_list=>[]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:puts, [] ,Ast::StringExpression.new("hello"))
|
||||
@transform_output = s(:call, s(:name, "puts"), [], s(:string, "hello"))
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_single_class
|
||||
@string_input = 'Object.foo(42)'
|
||||
@parse_output = {:receiver=>{:module_name=>"Object"}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::ModuleName.new("Object"))
|
||||
@transform_output = s(:call, s(:name, "foo"), [s(:int, 42)], s(:module, "Object"))
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
@ -59,59 +53,52 @@ class TestCallSite < MiniTest::Test
|
||||
@parse_output = {:call_site => {:name => 'baz' },
|
||||
:argument_list => [{:argument => {:integer => '42'}},
|
||||
{:argument => {:name => 'foo'}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new :baz,
|
||||
[Ast::IntegerExpression.new(42), Ast::NameExpression.new("foo") ]
|
||||
@transform_output = s(:call, s(:name, "baz"), [s(:int, 42), s(:name, "foo")])
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_call_site_string
|
||||
@string_input = 'puts( "hello")'
|
||||
@parse_output = {:call_site => {:name => 'puts' },
|
||||
:argument_list => [{:argument =>
|
||||
:argument_list => [{:argument =>
|
||||
{:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new "puts", [Ast::StringExpression.new("hello")]
|
||||
@transform_output = s(:call, s(:name, "puts"), [s(:string, "hello")])
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_call_operator
|
||||
@string_input = 'puts( 3 + 5)'
|
||||
@parse_output = {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"5"}}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:puts, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(5))] )
|
||||
@transform_output = s(:call, s(:name, "puts"), [s(:operator, "+", s(:int, 3), s(:int, 5))])
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_call_two_operators
|
||||
@string_input = 'puts(3 + 5 , a - 3)'
|
||||
@parse_output = {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"5"}}}, {:argument=>{:l=>{:name=>"a"}, :o=>"- ", :r=>{:integer=>"3"}}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:puts, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(3))] )
|
||||
@transform_output = s(:call, s(:name, "puts"), [s(:operator, "+", s(:int, 3), s(:int, 5)), s(:operator, "-", s(:name, "a"), s(:int, 3))])
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_call_chaining
|
||||
@string_input = 'puts(putint(3 + 5 ), a - 3)'
|
||||
@parse_output = {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:call_site=>{:name=>"putint"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"5"}}}]}}, {:argument=>{:l=>{:name=>"a"}, :o=>"- ", :r=>{:integer=>"3"}}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:puts, [Ast::CallSiteExpression.new(:putint, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(5))] ),Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(3))] )
|
||||
@transform_output = s(:call, s(:name, "puts"), [s(:call, s(:name, "putint"), [s(:operator, "+", s(:int, 3), s(:int, 5))]), s(:operator, "-", s(:name, "a"), s(:int, 3))])
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_call_chaining_name
|
||||
@string_input = 'puts(name.putint(4), a)'
|
||||
@parse_output = {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:receiver=>{:name=>"name"}, :call_site=>{:name=>"putint"}, :argument_list=>[{:argument=>{:integer=>"4"}}]}}, {:argument=>{:name=>"a"}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:puts, [Ast::CallSiteExpression.new(:putint, [Ast::IntegerExpression.new(4)] ,Ast::NameExpression.new("name")),Ast::NameExpression.new("a")] ,Ast::NameExpression.new("self"))
|
||||
@transform_output = s(:call, s(:name, "puts"), [s(:call, s(:name, "putint"), [s(:int, 4)], s(:name, "name")), s(:name, "a")])
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
def test_call_chaining_class
|
||||
@string_input = 'Class.new(self.get(4))'
|
||||
@parse_output = {:receiver=>{:module_name=>"Class"}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:receiver=>{:name=>"self"}, :call_site=>{:name=>"get"}, :argument_list=>[{:argument=>{:integer=>"4"}}]}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:new, [Ast::CallSiteExpression.new(:get, [Ast::IntegerExpression.new(4)] ,Ast::NameExpression.new("self"))] ,Ast::ModuleName.new("Class"))
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
def test_call_chaining_instance
|
||||
@string_input = '@class.new(self.get(4))'
|
||||
@parse_output = {:receiver=>{:instance_variable=>{:name=>"class"}}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:receiver=>{:name=>"self"}, :call_site=>{:name=>"get"}, :argument_list=>[{:argument=>{:integer=>"4"}}]}}]}
|
||||
@transform_output = Ast::CallSiteExpression.new(:new, [Ast::CallSiteExpression.new(:get, [Ast::IntegerExpression.new(4)] ,Ast::NameExpression.new("self"))] ,Ast::VariableExpression.new(:class))
|
||||
@transform_output = s(:call, s(:name, "new"), [s(:call, s(:name, "get"), [s(:int, 4)], s(:name, "self"))], s(:module, "Class"))
|
||||
@parser = @parser.call_site
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -3,7 +3,7 @@ require_relative "../parser_helper"
|
||||
class TestClassDef < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_simplest_class
|
||||
@string_input = <<HERE
|
||||
class Foo
|
||||
@ -11,7 +11,7 @@ class Foo
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Foo", :derived_name=>nil, :class_expressions=>[{:integer=>"5"}], :end=>"end"}
|
||||
@transform_output = Ast::ClassExpression.new(:Foo ,nil, [Ast::IntegerExpression.new(5)] )
|
||||
@transform_output = s(:class, "Foo", nil, [s(:int, 5)])
|
||||
@parser = @parser.class_definition
|
||||
end
|
||||
|
||||
@ -19,13 +19,13 @@ HERE
|
||||
@string_input = <<HERE
|
||||
class Opers
|
||||
def foo(x)
|
||||
@abba = 5
|
||||
abba = 5
|
||||
2 + 5
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Opers", :derived_name=>nil, :class_expressions=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:l=>{:instance_variable=>{:name=>"abba"}}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ClassExpression.new(:Opers ,nil, [Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new(:x)] , [Ast::AssignmentExpression.new(Ast::VariableExpression.new(:abba),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] ,nil )] )
|
||||
@parse_output = {:module_name=>"Opers", :derived_name=>nil, :class_expressions=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = s(:class, "Opers", nil, [s(:function, s(:name, "foo"), [s(:name, "x")], [s(:assign, s(:name, "abba"), s(:int, 5)), s(:operator, "+", s(:int, 2), s(:int, 5))])])
|
||||
@parser = @parser.class_definition
|
||||
end
|
||||
|
||||
@ -42,7 +42,7 @@ class Ifi
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Ifi", :derived_name=>nil, :class_expressions=>[{:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{: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::ClassExpression.new(:Ifi ,nil, [Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new(:n)] , [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:isit),Ast::IntegerExpression.new(42))],[Ast::AssignmentExpression.new(Ast::NameExpression.new(:maybenot),Ast::IntegerExpression.new(667))] )] ,nil )] )
|
||||
@transform_output = s(:class, "Ifi", nil, [s(:function, s(:name, "ofthen"), [s(:name, "n")], [s(:if, s(:int, 0), [s(:assign, s(:name, "isit"), s(:int, 42))], [s(:assign, s(:name, "maybenot"), s(:int, 667))])])])
|
||||
@parser = @parser.class_definition
|
||||
end
|
||||
|
||||
@ -56,7 +56,7 @@ class Pifi
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Pifi", :derived_name=>nil, :class_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}, {:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{:name=>"n"}}, {:parameter=>{:name=>"m"}}], :expressions=>[{:integer=>"44"}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ClassExpression.new(:Pifi ,nil, [Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ,Ast::NameExpression.new(:self)), Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new(:n),Ast::NameExpression.new(:m)] , [Ast::IntegerExpression.new(44)] ,nil )] )
|
||||
@transform_output = s(:class, "Pifi", nil, [s(:call, s(:name, "ofthen"), [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:name, "var")]), s(:function, s(:name, "ofthen"), [s(:name, "n"), s(:name, "m")], [s(:int, 44)])])
|
||||
@parser = @parser.class_definition
|
||||
end
|
||||
def test_class_module
|
||||
@ -68,7 +68,7 @@ class Foo
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Foo", :derived_name=>nil, :class_expressions=>[{:module_name=>"Boo", :module_expressions=>[{:call_site=>{:name=>"funcall"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ClassExpression.new(:Foo ,nil, [Ast::ModuleExpression.new(:Boo ,[Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ,Ast::NameExpression.new(:self))] )] )
|
||||
@transform_output = s(:class, "Foo", nil, [s(:module, "Boo", [s(:call, s(:name, "funcall"), [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:name, "var")])])])
|
||||
@parser = @parser.class_definition
|
||||
end
|
||||
def test_class_derived
|
||||
@ -78,7 +78,7 @@ class Foo < Object
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Foo", :derived_name=>{:module_name=>"Object"}, :class_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}], :end=>"end"}
|
||||
@transform_output = Ast::ClassExpression.new(:Foo ,:Object, [Ast::CallSiteExpression.new(:ofthen, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ,Ast::NameExpression.new(:self))] )
|
||||
@transform_output = s(:class, "Foo", s(:module, "Object"), [s(:call, s(:name, "ofthen"), [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:name, "var")])])
|
||||
@parser = @parser.class_definition
|
||||
end
|
||||
def test_class_method
|
||||
@ -90,8 +90,8 @@ class Foo < Object
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Foo", :derived_name=>{:module_name=>"Object"}, :class_expressions=>[{:receiver=>{:module_name=>"Foo"}, :function_name=>{:name=>"test"}, :parameter_list=>[], :expressions=>[{:integer=>"43"}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ClassExpression.new(:Foo ,:Object, [Ast::FunctionExpression.new(:test, [] , [Ast::IntegerExpression.new(43)] ,Ast::ModuleName.new(:Foo) )] )
|
||||
@transform_output = s(:class, "Foo", s(:module, "Object"), [s(:function, s(:name, "test"), [], [s(:int, 43)], s(:module, "Foo"))])
|
||||
@parser = @parser.class_definition
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -3,49 +3,47 @@ require_relative "../parser_helper"
|
||||
class TestCompound < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_one_array
|
||||
@string_input = '[42]'
|
||||
@parse_output = {:array_constant=>[{:array_element=>{:integer=>"42"}}]}
|
||||
@transform_output = Ast::ArrayExpression.new([Ast::IntegerExpression.new(42)])
|
||||
@transform_output = s(:array, [s(:int, 42)])
|
||||
@parser = @parser.array_constant
|
||||
end
|
||||
|
||||
def test_array_list
|
||||
@string_input = '[42, foo]'
|
||||
@parse_output = {:array_constant=>[{:array_element=>{:integer=>"42"}}, {:array_element=>{:name=>"foo"}}]}
|
||||
@transform_output = Ast::ArrayExpression.new([Ast::IntegerExpression.new(42), Ast::NameExpression.new("foo")])
|
||||
@transform_output = s(:array, [s(:int, 42), s(:name, "foo")])
|
||||
@parser = @parser.array_constant
|
||||
end
|
||||
|
||||
def test_array_list2
|
||||
@string_input = '[42, nil]'
|
||||
@parse_output = {:array_constant=>[{:array_element=>{:integer=>"42"}}, {:array_element=>{:nil=>"nil"}}]}
|
||||
@transform_output = Ast::ArrayExpression.new([Ast::IntegerExpression.new(42), Ast::NilExpression.new()])
|
||||
@transform_output = s(:array, [s(:int, 42), s(:nil)])
|
||||
@parser = @parser.array_constant
|
||||
end
|
||||
|
||||
def test_array_ops
|
||||
@string_input = '[ 3 + 4 , foo(22) ]'
|
||||
@parse_output = {:array_constant=>[{:array_element=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"4"}}}, {:array_element=>{:call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"22"}}]}}]}
|
||||
@transform_output = Ast::ArrayExpression.new(
|
||||
[Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),
|
||||
Ast::CallSiteExpression.new("foo", [Ast::IntegerExpression.new(22)] )])
|
||||
@transform_output = s(:array, [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:call, s(:name, "foo"), [s(:int, 22)])])
|
||||
@parser = @parser.array_constant
|
||||
end
|
||||
|
||||
def test_hash
|
||||
@string_input = '{ foo => 33 }'
|
||||
@parse_output = {:hash_constant=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:integer=>"33"}}}]}
|
||||
@transform_output = Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new("foo") , Ast::IntegerExpression.new(33))])
|
||||
@transform_output = s(:hash, [s(:assoc, s(:name, "foo"), s(:int, 33))])
|
||||
@parser = @parser.hash_constant
|
||||
end
|
||||
|
||||
def test_hash_list
|
||||
@string_input = "{foo => 33 , bar => 42}"
|
||||
@parse_output = {:hash_constant=>[{:hash_pair=>{:hash_key=>{:name=>"foo"}, :hash_value=>{:integer=>"33"}}}, {:hash_pair=>{:hash_key=>{:name=>"bar"}, :hash_value=>{:integer=>"42"}}}]}
|
||||
@transform_output = Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new("foo") , Ast::IntegerExpression.new(33)),Ast::AssociationExpression.new(Ast::NameExpression.new("bar") , Ast::IntegerExpression.new(42))])
|
||||
@transform_output = s(:hash, [s(:assoc, s(:name, "foo"), s(:int, 33)), s(:assoc, s(:name, "bar"), s(:int, 42))])
|
||||
@parser = @parser.hash_constant
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -4,24 +4,16 @@ class TestConditional < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
def test_conditional_brackets
|
||||
check("(0)")
|
||||
end
|
||||
def test_conditional_no_brackets
|
||||
check("0")
|
||||
end
|
||||
|
||||
def check cond
|
||||
input = <<HERE
|
||||
|
||||
42
|
||||
def test_basic_if
|
||||
@string_input = <<HERE
|
||||
if( 1 )
|
||||
return 42
|
||||
else
|
||||
667
|
||||
end
|
||||
HERE
|
||||
@string_input = "if #{cond} " + input.chop!
|
||||
@parse_output = {:if=>"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}
|
||||
@transform_output = Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::IntegerExpression.new(42)],[Ast::IntegerExpression.new(667)] )
|
||||
@parse_output = {:if=>"if", :conditional=>{:integer=>"1"}, :if_true=>{:expressions=>[{:return=>"return", :return_expression=>{:integer=>"42"}}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}
|
||||
@transform_output = s(:if, s(:int, 1), [s(:return, s(:int, 42))], [s(:int, 667)])
|
||||
@parser = @parser.conditional
|
||||
end
|
||||
|
||||
@ -35,7 +27,7 @@ end
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:if=>"if", :conditional=>{:l=>{:integer=>"3"}, :o=>"> ", :r=>{:name=>"var"}}, :if_true=>{:expressions=>[{:receiver=>{:module_name=>"Object"}, :call_site=>{:name=>"initialize"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}], :else=>"else"}, :if_false=>{:expressions=>[{:receiver=>{:name=>"var"}, :call_site=>{:name=>"new"}, :argument_list=>[{:argument=>{:integer=>"33"}}]}], :end=>"end"}}
|
||||
@transform_output = Ast::IfExpression.new(Ast::OperatorExpression.new(">", Ast::IntegerExpression.new(3),Ast::NameExpression.new("var")), [Ast::CallSiteExpression.new(:initialize, [Ast::IntegerExpression.new(3)] ,Ast::ModuleName.new("Object"))],[Ast::CallSiteExpression.new(:new, [Ast::IntegerExpression.new(33)] ,Ast::NameExpression.new("var"))] )
|
||||
@transform_output = s(:if, s(:operator, ">", s(:int, 3), s(:name, "var")), [s(:call, s(:name, "initialize"), [s(:int, 3)], s(:module, "Object"))], [s(:call, s(:name, "new"), [s(:int, 33)], s(:name, "var"))])
|
||||
@parser = @parser.conditional
|
||||
end
|
||||
|
||||
@ -49,26 +41,18 @@ end
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:if=>"if", :conditional=>{:l=>{:integer=>"3"}, :o=>"== ", :r=>{:nil=>"nil"}}, :if_true=>{:expressions=>[{:integer=>"3"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"4"}], :end=>"end"}}
|
||||
@transform_output = Ast::IfExpression.new(Ast::OperatorExpression.new("==", Ast::IntegerExpression.new(3),Ast::NilExpression.new()), [Ast::IntegerExpression.new(3)],[Ast::IntegerExpression.new(4)] )
|
||||
@transform_output = s(:if, s(:operator, "==", s(:int, 3), s(:nil)), [s(:int, 3)], [s(:int, 4)])
|
||||
@parser = @parser.conditional
|
||||
end
|
||||
|
||||
def pest_simple_if
|
||||
def test_small_if
|
||||
@string_input = <<HERE
|
||||
if(3 == nil)
|
||||
3
|
||||
if(0)
|
||||
42
|
||||
end
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:if=>"if", :conditional=>{:l=>{:integer=>"3"}, :o=>"== ", :r=>{:nil=>"nil"}}, :if_true=>{:expressions=>[{:integer=>"3"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"4"}], :end=>"end"}}
|
||||
@transform_output = Ast::IfExpression.new(Ast::OperatorExpression.new("==", Ast::IntegerExpression.new(3),Ast::NilExpression.new()), [Ast::IntegerExpression.new(3)],[Ast::IntegerExpression.new(4)] )
|
||||
@parser = @parser.conditional
|
||||
end
|
||||
|
||||
def pest_reverse_if
|
||||
@string_input = "3 if(3 == nil)"
|
||||
@parse_output = {:if=>"if", :conditional=>{:l=>{:integer=>"3"}, :o=>"== ", :r=>{:nil=>"nil"}}, :if_true=>{:expressions=>[{:integer=>"3"}], :else=>"else"}, :if_false=>{:expressions=>[{:integer=>"4"}], :end=>"end"}}
|
||||
@transform_output = Ast::IfExpression.new(Ast::OperatorExpression.new("==", Ast::IntegerExpression.new(3),Ast::NilExpression.new()), [Ast::IntegerExpression.new(3)],[Ast::IntegerExpression.new(4)] )
|
||||
@parser = @parser.conditional
|
||||
@parse_output = {:expression_list=>[{:if=>"if", :conditional=>{:integer=>"0"}, :if_true=>{:expressions=>[{:integer=>"42"}], :end=>"end"}}]}
|
||||
@transform_output = s(:list, [s(:if, s(:int, 0), [s(:int, 42)], nil)])
|
||||
@parser = @parser
|
||||
end
|
||||
end
|
||||
|
@ -3,7 +3,7 @@ require_relative "../parser_helper"
|
||||
class TestExpressions < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_expression_else
|
||||
@string_input = <<HERE
|
||||
dud
|
||||
@ -11,11 +11,10 @@ fuu(3)
|
||||
else
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:expressions=>[{:name=>"dud"},
|
||||
{:call_site=>{:name=>"fuu"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}],
|
||||
@parse_output = {:expressions=>[{:name=>"dud"},
|
||||
{:call_site=>{:name=>"fuu"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}],
|
||||
:else=>"else"}
|
||||
@transform_output ={:expressions=>[Ast::NameExpression.new("dud"),
|
||||
Ast::CallSiteExpression.new("fuu", [Ast::IntegerExpression.new(3)] )], :else=>"else"}
|
||||
@transform_output = {:expressions=>[s(:name, "dud"), s(:call, s(:name, "fuu"), [s(:int, 3)])], :else=>"else"}
|
||||
@parser = @parser.expressions_else
|
||||
end
|
||||
|
||||
@ -26,15 +25,12 @@ call(4,6)
|
||||
end
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:expressions=>[{:name=>"name"},
|
||||
{:call_site=>{:name=>"call"}, :argument_list=>[{:argument=>{:integer=>"4"}}, {:argument=>{:integer=>"6"}}]}],
|
||||
@parse_output = {:expressions=>[{:name=>"name"},
|
||||
{:call_site=>{:name=>"call"}, :argument_list=>[{:argument=>{:integer=>"4"}}, {:argument=>{:integer=>"6"}}]}],
|
||||
:end=>"end"}
|
||||
@transform_output = {:expressions=>[Ast::NameExpression.new("name"),
|
||||
Ast::CallSiteExpression.new("call", [Ast::IntegerExpression.new(4),Ast::IntegerExpression.new(6)] )],
|
||||
:end=>"end"}
|
||||
|
||||
@transform_output = {:expressions=>[s(:name, "name"), s(:call, s(:name, "call"), [s(:int, 4), s(:int, 6)])], :end=>"end"}
|
||||
@parser = @parser.expressions_end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -1,26 +0,0 @@
|
||||
require_relative "../parser_helper"
|
||||
|
||||
# some cases that fail, and fail badly.
|
||||
|
||||
# These make the parse "hang", ie there is some looping going on in the parser, but not straight down, as theey don't
|
||||
# throw even StackError
|
||||
|
||||
# Annoyingly, the user error is quite small, a missing bracket or things
|
||||
|
||||
class TestFails < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
def test_fail_function
|
||||
@string_input = <<HERE
|
||||
class Foo
|
||||
def bar
|
||||
4
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = nil
|
||||
@transform_output = nil
|
||||
@parser = @parser.root
|
||||
end
|
||||
end
|
@ -3,38 +3,38 @@ require_relative "../parser_helper"
|
||||
class TestFunctionDefinition < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_simplest_function
|
||||
@string_input = <<HERE
|
||||
def foo(x)
|
||||
def foo(x)
|
||||
5
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:integer=>"5"}], :end=>"end"}
|
||||
@transform_output = Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::IntegerExpression.new(5)] )
|
||||
@transform_output = s(:function, s(:name, "foo"), [s(:name, "x")], [s(:int, 5)])
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
|
||||
def test_class_function
|
||||
@string_input = <<HERE
|
||||
def String.length(x)
|
||||
@length
|
||||
length
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:receiver=>{:module_name=>"String"}, :function_name=>{:name=>"length"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:instance_variable=>{:name=>"length"}}], :end=>"end"}
|
||||
@transform_output = Ast::FunctionExpression.new(:length, [Ast::NameExpression.new("x")] , [Ast::VariableExpression.new(:length)] ,Ast::ModuleName.new("String") )
|
||||
@parse_output = {:receiver=>{:module_name=>"String"}, :function_name=>{:name=>"length"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:name=>"length"}], :end=>"end"}
|
||||
@transform_output = s(:function, s(:name, "length"), [s(:name, "x")], [s(:name, "length")], s(:module, "String"))
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
|
||||
def test_function_ops
|
||||
@string_input = <<HERE
|
||||
def foo(x)
|
||||
abba = 5
|
||||
def foo(x)
|
||||
abba = 5
|
||||
2 + 5
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{:name=>"x"}}], :expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}, {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"5"}}], :end=>"end"}
|
||||
@transform_output = Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new(:x)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:abba),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] ,nil )
|
||||
@transform_output = s(:function, s(:name, "foo"), [s(:name, "x")], [s(:assign, s(:name, "abba"), s(:int, 5)), s(:operator, "+", s(:int, 2), s(:int, 5))])
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
|
||||
@ -49,7 +49,7 @@ def ofthen(n)
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{: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"}
|
||||
@transform_output = Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new(:n)] , [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:isit),Ast::IntegerExpression.new(42))],[Ast::AssignmentExpression.new(Ast::NameExpression.new(:maybenot),Ast::IntegerExpression.new(667))] )] ,nil )
|
||||
@transform_output = s(:function, s(:name, "ofthen"), [s(:name, "n")], [s(:if, s(:int, 0), [s(:assign, s(:name, "isit"), s(:int, 42))], [s(:assign, s(:name, "maybenot"), s(:int, 667))])])
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
|
||||
@ -57,11 +57,11 @@ HERE
|
||||
@string_input = <<HERE
|
||||
def retvar(n)
|
||||
i = 5
|
||||
return i
|
||||
return i
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"i"}, :o=>"= ", :r=>{:integer=>"5"}}, {:return=>"return", :return_expression=>{:name=>"i"}}], :end=>"end"}
|
||||
@transform_output = Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:i),Ast::IntegerExpression.new(5)),Ast::ReturnExpression.new(Ast::NameExpression.new(:i) )] ,nil )
|
||||
@transform_output = s(:function, s(:name, "retvar"), [s(:name, "n")], [s(:assign, s(:name, "i"), s(:int, 5)), s(:return, s(:name, "i"))])
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
|
||||
@ -72,25 +72,25 @@ def retvar(n)
|
||||
return 10
|
||||
else
|
||||
return 20
|
||||
end
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:if=>"if", :conditional=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"5"}}, :if_true=>{:expressions=>[{:return=>"return", :return_expression=>{:integer=>"10"}}], :else=>"else"}, :if_false=>{:expressions=>[{:return=>"return", :return_expression=>{:integer=>"20"}}], :end=>"end"}}], :end=>"end"}
|
||||
@transform_output = Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new("n")] , [Ast::IfExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(5)), [Ast::ReturnExpression.new(Ast::IntegerExpression.new(10) )],[Ast::ReturnExpression.new(Ast::IntegerExpression.new(20) )] )] )
|
||||
@transform_output = s(:function, s(:name, "retvar"), [s(:name, "n")], [s(:if, s(:operator, ">", s(:name, "n"), s(:int, 5)), [s(:return, s(:int, 10))], [s(:return, s(:int, 20))])])
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
|
||||
def test_function_return_while
|
||||
@string_input = <<HERE
|
||||
def retvar(n)
|
||||
while( n > 5) do
|
||||
while( n > 5)
|
||||
n = n + 1
|
||||
return n
|
||||
end
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"5"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"+ ", :r=>{:integer=>"1"}}}, {:return=>"return", :return_expression=>{:name=>"n"}}], :end=>"end"}}], :end=>"end"}
|
||||
@transform_output = Ast::FunctionExpression.new(:retvar, [Ast::NameExpression.new(:n)] , [Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(5)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1))), Ast::ReturnExpression.new(Ast::NameExpression.new(:n) )] )] ,nil )
|
||||
@parse_output = {:function_name=>{:name=>"retvar"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"5"}}, :body=>{:expressions=>[{:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"+ ", :r=>{:integer=>"1"}}}, {:return=>"return", :return_expression=>{:name=>"n"}}], :end=>"end"}}], :end=>"end"}
|
||||
@transform_output = s(:function, s(:name, "retvar"), [s(:name, "n")], [s(:while, s(:operator, ">", s(:name, "n"), s(:int, 5)), [s(:assign, s(:name, "n"), s(:operator, "+", s(:name, "n"), s(:int, 1))), s(:return, s(:name, "n"))])])
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
|
||||
@ -98,23 +98,23 @@ HERE
|
||||
@string_input = <<HERE
|
||||
def fibonaccit(n)
|
||||
a = 0
|
||||
while (n) do
|
||||
while(n)
|
||||
some = 43
|
||||
other = some * 4
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:while=>"while", :while_cond=>{:name=>"n"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"some"}, :o=>"= ", :r=>{:integer=>"43"}}, {:l=>{:name=>"other"}, :o=>"= ", :r=>{:l=>{:name=>"some"}, :o=>"* ", :r=>{:integer=>"4"}}}], :end=>"end"}}], :end=>"end"}
|
||||
@transform_output = Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(0)),Ast::WhileExpression.new(Ast::NameExpression.new(:n), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:some),Ast::IntegerExpression.new(43)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:other),Ast::OperatorExpression.new("*", Ast::NameExpression.new(:some),Ast::IntegerExpression.new(4)))] )] ,nil )
|
||||
@parse_output = {:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:while=>"while", :while_cond=>{:name=>"n"}, :body=>{:expressions=>[{:l=>{:name=>"some"}, :o=>"= ", :r=>{:integer=>"43"}}, {:l=>{:name=>"other"}, :o=>"= ", :r=>{:l=>{:name=>"some"}, :o=>"* ", :r=>{:integer=>"4"}}}], :end=>"end"}}], :end=>"end"}
|
||||
@transform_output = s(:function, s(:name, "fibonaccit"), [s(:name, "n")], [s(:assign, s(:name, "a"), s(:int, 0)), s(:while, s(:name, "n"), [s(:assign, s(:name, "some"), s(:int, 43)), s(:assign, s(:name, "other"), s(:operator, "*", s(:name, "some"), s(:int, 4)))])])
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
|
||||
def test_function_big_while
|
||||
@string_input = <<HERE
|
||||
def fibonaccit(n)
|
||||
a = 0
|
||||
a = 0
|
||||
b = 1
|
||||
while( n > 1 ) do
|
||||
while( n > 1 )
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp + b
|
||||
@ -123,8 +123,8 @@ def fibonaccit(n)
|
||||
end
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:integer=>"1"}}, {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}], :end=>"end"}
|
||||
@transform_output = Ast::FunctionExpression.new(:fibonaccit, [Ast::NameExpression.new(:n)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(0)),Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::IntegerExpression.new(1)),Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::NameExpression.new(:b)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:b))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)))] )] ,nil )
|
||||
@parse_output = {:function_name=>{:name=>"fibonaccit"}, :parameter_list=>[{:parameter=>{:name=>"n"}}], :expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"0"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:integer=>"1"}}, {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}], :end=>"end"}
|
||||
@transform_output = s(:function, s(:name, "fibonaccit"), [s(:name, "n")], [s(:assign, s(:name, "a"), s(:int, 0)), s(:assign, s(:name, "b"), s(:int, 1)), s(:while, s(:operator, ">", s(:name, "n"), s(:int, 1)), [s(:assign, s(:name, "tmp"), s(:name, "a")), s(:assign, s(:name, "a"), s(:name, "b")), s(:assign, s(:name, "b"), s(:operator, "+", s(:name, "tmp"), s(:name, "b"))), s(:call, s(:name, "puts"), [s(:name, "b")]), s(:assign, s(:name, "n"), s(:operator, "-", s(:name, "n"), s(:int, 1)))])])
|
||||
@parser = @parser.function_definition
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -3,7 +3,7 @@ require_relative "../parser_helper"
|
||||
class TestModuleDef < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_simplest_module
|
||||
@string_input = <<HERE
|
||||
module Simple
|
||||
@ -11,7 +11,7 @@ module Simple
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Simple", :module_expressions=>[{:integer=>"5"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:Simple ,[Ast::IntegerExpression.new(5)] )
|
||||
@transform_output = s(:module, "Simple", [s(:int, 5)])
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
@ -25,18 +25,18 @@ module Opers
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Opers", :module_expressions=>[{:function_name=>{:name=>"foo"}, :parameter_list=>[{:parameter=>{: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(:Opers ,[Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new(:x)] , [Ast::AssignmentExpression.new(Ast::NameExpression.new(:abba),Ast::IntegerExpression.new(5)),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(5))] ,nil )] )
|
||||
@transform_output = s(:module, "Opers", [s(:function, s(:name, "foo"), [s(:name, "x")], [s(:assign, s(:name, "abba"), s(:int, 5)), s(:operator, "+", s(:int, 2), s(:int, 5))])])
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
def test_module_assign_instance
|
||||
@string_input = <<HERE
|
||||
module Opers
|
||||
@abba = 5
|
||||
abba = 5
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Opers", :module_expressions=>[{:l=>{:instance_variable=>{:name=>"abba"}}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:Opers ,[Ast::AssignmentExpression.new(Ast::VariableExpression.new(:abba),Ast::IntegerExpression.new(5))] )
|
||||
@parse_output = {:module_name=>"Opers", :module_expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}
|
||||
@transform_output = s(:module, "Opers", [s(:assign, s(:name, "abba"), s(:int, 5))])
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
@ -53,7 +53,7 @@ module Foo
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Foo", :module_expressions=>[{:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{: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(:Foo ,[Ast::FunctionExpression.new(:ofthen, [Ast::NameExpression.new(:n)] , [Ast::IfExpression.new(Ast::IntegerExpression.new(0), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:isit),Ast::IntegerExpression.new(42))],[Ast::AssignmentExpression.new(Ast::NameExpression.new(:maybenot),Ast::IntegerExpression.new(667))] )] ,nil )] )
|
||||
@transform_output = s(:module, "Foo", [s(:function, s(:name, "ofthen"), [s(:name, "n")], [s(:if, s(:int, 0), [s(:assign, s(:name, "isit"), s(:int, 42))], [s(:assign, s(:name, "maybenot"), s(:int, 667))])])])
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
@ -67,7 +67,7 @@ module Soho
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Soho", :module_expressions=>[{:call_site=>{:name=>"ofthen"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}, {:function_name=>{:name=>"ofthen"}, :parameter_list=>[{:parameter=>{:name=>"n"}}, {:parameter=>{:name=>"m"}}], :expressions=>[{:integer=>"44"}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:Soho ,[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)] )] )
|
||||
@transform_output = s(:module, "Soho", [s(:call, s(:name, "ofthen"), [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:name, "var")]), s(:function, s(:name, "ofthen"), [s(:name, "n"), s(:name, "m")], [s(:int, 44)])])
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
|
||||
@ -80,7 +80,7 @@ module Foo
|
||||
end
|
||||
HERE
|
||||
@parse_output = {:module_name=>"Foo", :module_expressions=>[{:module_name=>"Bar", :derived_name=>nil, :class_expressions=>[{:call_site=>{:name=>"funcall"}, :argument_list=>[{:argument=>{:l=>{:integer=>"3"}, :o=>"+", :r=>{:integer=>"4"}}}, {:argument=>{:name=>"var"}}]}], :end=>"end"}], :end=>"end"}
|
||||
@transform_output = Ast::ModuleExpression.new(:Foo ,[Ast::ClassExpression.new(:Bar ,nil, [Ast::CallSiteExpression.new(:funcall, [Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),Ast::NameExpression.new(:var)] ,Ast::NameExpression.new(:self))] )] )
|
||||
@transform_output = s(:module, "Foo", [s(:class, "Bar", nil, [s(:call, s(:name, "funcall"), [s(:operator, "+", s(:int, 3), s(:int, 4)), s(:name, "var")])])])
|
||||
@parser = @parser.module_definition
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -7,7 +7,7 @@ class TestExpressions < MiniTest::Test
|
||||
def simple_op op
|
||||
@string_input = "5 #{op} 3"
|
||||
@parse_output = {:l=>{:integer=>"5"}, :o=>"#{op} ", :r=>{:integer=>"3"}}
|
||||
@transform_output = Ast::OperatorExpression.new(op, Ast::IntegerExpression.new(5),Ast::IntegerExpression.new(3))
|
||||
@transform_output = s(:operator, op , s(:int, 5), s(:int, 3))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_simple_multiply
|
||||
@ -31,56 +31,50 @@ class TestExpressions < MiniTest::Test
|
||||
def test_op_variable
|
||||
@string_input = "a + 35"
|
||||
@parse_output = {:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}}
|
||||
@transform_output = Ast::OperatorExpression.new("+", Ast::NameExpression.new(:a),Ast::IntegerExpression.new(35))
|
||||
@transform_output = s(:operator, "+", s(:name, "a"), s(:int, 35))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_op_two_variable
|
||||
@string_input = "a - b"
|
||||
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:name=>"b"}}
|
||||
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::NameExpression.new("b"))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_op_instance_variable
|
||||
@string_input = "@a - 5"
|
||||
@parse_output = {:l=>{:instance_variable=>{:name=>"a"}}, :o=>"- ", :r=>{:integer=>"5"}}
|
||||
@transform_output = Ast::OperatorExpression.new("-", Ast::VariableExpression.new(:a),Ast::IntegerExpression.new(5))
|
||||
@transform_output = s(:operator, "-", s(:name, "a"), s(:name, "b"))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_op_variable_string
|
||||
@string_input = 'a - "st"'
|
||||
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}}
|
||||
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::StringExpression.new("st"))
|
||||
@transform_output = s(:operator, "-", s(:name, "a"), s(:string, "st"))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_two_same_ops
|
||||
@string_input = '2 + 3 + 4'
|
||||
@parse_output = {:l=>{:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}
|
||||
@transform_output = Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4))
|
||||
@transform_output = s(:operator, "+", s(:operator, "+", s(:int, 2), s(:int, 3)), s(:int, 4))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_two_different_ops
|
||||
@string_input = '2 + 3 * 4'
|
||||
@parse_output = {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:l=>{:integer=>"3"}, :o=>"* ", :r=>{:integer=>"4"}}}
|
||||
@transform_output = Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)))
|
||||
@transform_output = s(:operator, "+", s(:int, 2), s(:operator, "*", s(:int, 3), s(:int, 4)))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_two_different_ops_order
|
||||
@string_input = '2 * 3 + 4'
|
||||
@parse_output = {:l=>{:l=>{:integer=>"2"}, :o=>"* ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}
|
||||
@transform_output = Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4))
|
||||
@transform_output = s(:operator, "+", s(:operator, "*", s(:int, 2), s(:int, 3)), s(:int, 4))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_assignment
|
||||
@string_input = "a = 5"
|
||||
@parse_output = {:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}
|
||||
@transform_output = Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(5))
|
||||
@transform_output = s(:assign, s(:name, "a"), s(:int, 5))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
def test_assignment_instance
|
||||
@string_input = "@a = 5"
|
||||
@parse_output = {:l=>{:instance_variable=>{:name=>"a"}}, :o=>"= ", :r=>{:integer=>"5"}}
|
||||
@transform_output = Ast::AssignmentExpression.new(Ast::VariableExpression.new(:a),Ast::IntegerExpression.new(5))
|
||||
@string_input = "a = 5"
|
||||
@parse_output = {:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}
|
||||
@transform_output = s(:assign, s(:name, "a"), s(:int, 5))
|
||||
@parser = @parser.operator_expression
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -3,33 +3,33 @@ require_relative "../parser_helper"
|
||||
class TestReturn < MiniTest::Test
|
||||
# include the magic (setup and parse -> test method translation), see there
|
||||
include ParserHelper
|
||||
|
||||
|
||||
def test_return_int
|
||||
@string_input = 'return 42'
|
||||
@parse_output = {:return=>"return", :return_expression=>{:integer=>"42"}}
|
||||
@transform_output = Ast::ReturnExpression.new(Ast::IntegerExpression.new(42) )
|
||||
@transform_output = s(:return, s(:int, 42))
|
||||
@parser = @parser.simple_return
|
||||
end
|
||||
|
||||
def test_return_variable
|
||||
@string_input = 'return foo'
|
||||
@parse_output = {:return=>"return", :return_expression=>{:name=>"foo"}}
|
||||
@transform_output = Ast::ReturnExpression.new(Ast::NameExpression.new("foo") )
|
||||
@transform_output = s(:return, s(:name, "foo"))
|
||||
@parser = @parser.simple_return
|
||||
end
|
||||
|
||||
def test_return_string
|
||||
@string_input = 'return "hello"'
|
||||
@parse_output = {:return=>"return", :return_expression=>{:string=>[{:char=>"h"}, {:char=>"e"}, {:char=>"l"}, {:char=>"l"}, {:char=>"o"}]}}
|
||||
@transform_output = Ast::ReturnExpression.new(Ast::StringExpression.new("hello") )
|
||||
@transform_output = s(:return, s(:string, "hello"))
|
||||
@parser = @parser.simple_return
|
||||
end
|
||||
|
||||
def test_return_true
|
||||
@string_input = 'return true'
|
||||
@parse_output = {:return=>"return", :return_expression=>{:true=>"true"}}
|
||||
@transform_output = Ast::ReturnExpression.new(Ast::TrueExpression.new() )
|
||||
@transform_output = s(:return, s(:true))
|
||||
@parser = @parser.simple_return
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -6,40 +6,33 @@ class TestWhile < MiniTest::Test
|
||||
|
||||
def test_while
|
||||
@string_input = <<HERE
|
||||
while(1) do
|
||||
while(1)
|
||||
tmp = a
|
||||
puts(b)
|
||||
end
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}], :end=>"end"}}
|
||||
@transform_output = Ast::WhileExpression.new(Ast::IntegerExpression.new(1), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self))] )
|
||||
@parser = @parser.while_do
|
||||
end
|
||||
|
||||
def pest_while_reverse
|
||||
@string_input = "puts '1' while true "
|
||||
@parse_output = {:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}], :end=>"end"}}
|
||||
@transform_output = Ast::WhileExpression.new(Ast::IntegerExpression.new(1), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self))] )
|
||||
@parse_output = {:while=>"while", :while_cond=>{:integer=>"1"}, :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}], :end=>"end"}}
|
||||
@transform_output = s(:while, s(:int, 1), [s(:assign, s(:name, "tmp"), s(:name, "a")), s(:call, s(:name, "puts"), [s(:name, "b")])])
|
||||
@parser = @parser.while_do
|
||||
end
|
||||
|
||||
def test_while_method
|
||||
@string_input = <<HERE
|
||||
while(1) do
|
||||
while(1)
|
||||
tmp = String.new()
|
||||
tmp.puts(i)
|
||||
end
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:receiver=>{:module_name=>"String"}, :call_site=>{:name=>"new"}, :argument_list=>[]}}, {:receiver=>{:name=>"tmp"}, :call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"i"}}]}], :end=>"end"}}
|
||||
@transform_output = Ast::WhileExpression.new(Ast::IntegerExpression.new(1), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::CallSiteExpression.new(:new, [] ,Ast::ModuleName.new(:String))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:i)] ,Ast::NameExpression.new(:tmp))] )
|
||||
@parse_output = {:while=>"while", :while_cond=>{:integer=>"1"}, :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:receiver=>{:module_name=>"String"}, :call_site=>{:name=>"new"}, :argument_list=>[]}}, {:receiver=>{:name=>"tmp"}, :call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"i"}}]}], :end=>"end"}}
|
||||
@transform_output = s(:while, s(:int, 1), [s(:assign, s(:name, "tmp"), s(:call, s(:name, "new"), [], s(:module, "String"))), s(:call, s(:name, "puts"), [s(:name, "i")], s(:name, "tmp"))])
|
||||
@parser = @parser.while_do
|
||||
end
|
||||
|
||||
def test_big_while
|
||||
@string_input = <<HERE
|
||||
while( n > 1) do
|
||||
while( n > 1)
|
||||
tmp = a
|
||||
a = b
|
||||
b = tmp + b
|
||||
@ -48,8 +41,8 @@ while( n > 1) do
|
||||
end
|
||||
HERE
|
||||
@string_input.chop!
|
||||
@parse_output = {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :do=>"do", :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}
|
||||
@transform_output = Ast::WhileExpression.new(Ast::OperatorExpression.new(">", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)), [Ast::AssignmentExpression.new(Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:a)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::NameExpression.new(:b)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:b),Ast::OperatorExpression.new("+", Ast::NameExpression.new(:tmp),Ast::NameExpression.new(:b))), Ast::CallSiteExpression.new(:puts, [Ast::NameExpression.new(:b)] ,Ast::NameExpression.new(:self)), Ast::AssignmentExpression.new(Ast::NameExpression.new(:n),Ast::OperatorExpression.new("-", Ast::NameExpression.new(:n),Ast::IntegerExpression.new(1)))] )
|
||||
@parse_output = {:while=>"while", :while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}}, :body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}, {:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}}, {:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}, {:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}], :end=>"end"}}
|
||||
@transform_output = s(:while, s(:operator, ">", s(:name, "n"), s(:int, 1)), [s(:assign, s(:name, "tmp"), s(:name, "a")), s(:assign, s(:name, "a"), s(:name, "b")), s(:assign, s(:name, "b"), s(:operator, "+", s(:name, "tmp"), s(:name, "b"))), s(:call, s(:name, "puts"), [s(:name, "b")]), s(:assign, s(:name, "n"), s(:operator, "-", s(:name, "n"), s(:int, 1)))])
|
||||
@parser = @parser.while_do
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user