adds basic values (bools ints strings)
This commit is contained in:
parent
b341c722fd
commit
8f03d98330
@ -3,7 +3,7 @@ module Vool
|
||||
attr_reader :name, :super_class_name , :body
|
||||
|
||||
def initialize( name , supe , body)
|
||||
@name , @super_class_name , @body = name , supe , (body || [])
|
||||
@name , @super_class_name , @body = name , supe , body
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -1,5 +1,5 @@
|
||||
module Vool
|
||||
class Compiler < ::Rubyx::Passes::TotalProcessor
|
||||
class Compiler < AST::Processor
|
||||
|
||||
def self.compile(input)
|
||||
ast = Parser::Ruby22.parse( input )
|
||||
@ -8,7 +8,7 @@ module Vool
|
||||
|
||||
def on_class( statement )
|
||||
name , sup , body = *statement
|
||||
ClassStatement.new( get_name(name) , get_name(sup) , body )
|
||||
ClassStatement.new( get_name(name) , get_name(sup) , process_all(body) )
|
||||
end
|
||||
|
||||
def on_def( statement )
|
||||
@ -21,6 +21,41 @@ module Vool
|
||||
arg.first
|
||||
end
|
||||
|
||||
def on_int expression
|
||||
IntegerStatement.new(expression.children.first)
|
||||
end
|
||||
|
||||
def on_float expression
|
||||
FloatStatement.new(expression.children.first)
|
||||
end
|
||||
|
||||
def on_true expression
|
||||
TrueStatement.new
|
||||
end
|
||||
|
||||
def on_false expression
|
||||
FalseStatement.new
|
||||
end
|
||||
|
||||
def on_nil expression
|
||||
NilStatement.new
|
||||
end
|
||||
|
||||
def on_str expression
|
||||
StringStatement.new(expression.children.first)
|
||||
end
|
||||
alias :on_string :on_str
|
||||
|
||||
def on_dstr
|
||||
raise "Not implemented dynamix strings (with interpolation)"
|
||||
end
|
||||
|
||||
def on_return statement
|
||||
w = ReturnStatement.new()
|
||||
w.return_value = process(statement.children.first)
|
||||
w
|
||||
end
|
||||
|
||||
def on_function statement
|
||||
return_type , name , parameters, statements , receiver = *statement
|
||||
w = FunctionStatement.new()
|
||||
@ -35,23 +70,6 @@ module Vool
|
||||
w
|
||||
end
|
||||
|
||||
def on_field_def statement
|
||||
type , name , value = *statement
|
||||
w = FieldDef.new()
|
||||
w.type = type
|
||||
w.name = process(name)
|
||||
w.value = process(value) if value
|
||||
w
|
||||
end
|
||||
|
||||
def on_class_field statement
|
||||
type , name = *statement
|
||||
w = ClassField.new()
|
||||
w.type = type
|
||||
w.name = name
|
||||
w
|
||||
end
|
||||
|
||||
def on_while_statement statement
|
||||
branch_type , condition , statements = *statement
|
||||
w = WhileStatement.new()
|
||||
@ -71,47 +89,15 @@ module Vool
|
||||
w
|
||||
end
|
||||
|
||||
def process_first code
|
||||
raise "Too many children #{code.inspect}" if code.children.length != 1
|
||||
process code.children.first
|
||||
end
|
||||
alias :on_conditional :process_first
|
||||
alias :on_condition :process_first
|
||||
alias :on_field :process_first
|
||||
|
||||
def on_statements statement
|
||||
w = Statements.new()
|
||||
return w unless statement.children
|
||||
return w unless statement.children.first
|
||||
w.statements = process_all(statement.children)
|
||||
w
|
||||
end
|
||||
alias :on_true_statements :on_statements
|
||||
alias :on_false_statements :on_statements
|
||||
|
||||
def on_return statement
|
||||
w = ReturnStatement.new()
|
||||
w.return_value = process(statement.children.first)
|
||||
w
|
||||
end
|
||||
|
||||
def on_operator_value statement
|
||||
operator , left_e , right_e = *statement
|
||||
w = OperatorExpression.new()
|
||||
w = OperatorStatement.new()
|
||||
w.operator = operator
|
||||
w.left_expression = process(left_e)
|
||||
w.right_expression = process(right_e)
|
||||
w
|
||||
end
|
||||
|
||||
def on_field_access statement
|
||||
receiver_ast , field_ast = *statement
|
||||
w = FieldAccess.new()
|
||||
w.receiver = process(receiver_ast)
|
||||
w.field = process(field_ast)
|
||||
w
|
||||
end
|
||||
|
||||
def on_receiver expression
|
||||
process expression.children.first
|
||||
end
|
||||
@ -125,31 +111,12 @@ module Vool
|
||||
w
|
||||
end
|
||||
|
||||
def on_int expression
|
||||
IntegerExpression.new(expression.children.first)
|
||||
end
|
||||
|
||||
def on_true expression
|
||||
TrueExpression.new
|
||||
end
|
||||
|
||||
def on_false expression
|
||||
FalseExpression.new
|
||||
end
|
||||
|
||||
def on_nil expression
|
||||
NilExpression.new
|
||||
end
|
||||
|
||||
def on_name statement
|
||||
NameExpression.new(statement.children.first)
|
||||
end
|
||||
def on_string expression
|
||||
StringExpression.new(expression.children.first)
|
||||
NameStatement.new(statement.children.first)
|
||||
end
|
||||
|
||||
def on_class_name expression
|
||||
ClassExpression.new(expression.children.first)
|
||||
ClassStatement.new(expression.children.first)
|
||||
end
|
||||
|
||||
def on_assignment statement
|
||||
|
@ -1,5 +0,0 @@
|
||||
module Vool
|
||||
class FieldAccess < Expression
|
||||
attr_accessor :receiver , :field
|
||||
end
|
||||
end
|
27
test/vool/test_basic_values.rb
Normal file
27
test/vool/test_basic_values.rb
Normal file
@ -0,0 +1,27 @@
|
||||
require_relative "../helper"
|
||||
|
||||
module Vool
|
||||
class TestBasicValues < MiniTest::Test
|
||||
|
||||
def test_nil
|
||||
lst = Compiler.compile( "nil")
|
||||
assert_equal NilStatement , lst.class
|
||||
end
|
||||
def test_false
|
||||
lst = Compiler.compile( "false")
|
||||
assert_equal FalseStatement , lst.class
|
||||
end
|
||||
def test_true
|
||||
lst = Compiler.compile( "true")
|
||||
assert_equal TrueStatement , lst.class
|
||||
end
|
||||
def test_integer
|
||||
lst = Compiler.compile( "123")
|
||||
assert_equal IntegerStatement , lst.class
|
||||
end
|
||||
def test_string
|
||||
lst = Compiler.compile( "'string'")
|
||||
assert_equal StringStatement , lst.class , lst.inspect
|
||||
end
|
||||
end
|
||||
end
|
@ -1,3 +1,4 @@
|
||||
require_relative "helper"
|
||||
require_relative "test_class_statement"
|
||||
require_relative "test_method_statement"
|
||||
require_relative "test_basic_values"
|
||||
|
Loading…
Reference in New Issue
Block a user