new field def compiles, fix test
This commit is contained in:
parent
bc70c1efe5
commit
f2fc9c5f89
@ -34,13 +34,14 @@ module Bosl
|
|||||||
end
|
end
|
||||||
|
|
||||||
require_relative "compiler/basic_expressions"
|
require_relative "compiler/basic_expressions"
|
||||||
require_relative "compiler/name_expression"
|
|
||||||
require_relative "compiler/callsite_expression"
|
require_relative "compiler/callsite_expression"
|
||||||
require_relative "compiler/compound_expressions"
|
require_relative "compiler/compound_expressions"
|
||||||
require_relative "compiler/if_expression"
|
require_relative "compiler/expression_list"
|
||||||
|
require_relative "compiler/field_def"
|
||||||
require_relative "compiler/function_expression"
|
require_relative "compiler/function_expression"
|
||||||
|
require_relative "compiler/if_expression"
|
||||||
require_relative "compiler/module_expression"
|
require_relative "compiler/module_expression"
|
||||||
|
require_relative "compiler/name_expression"
|
||||||
require_relative "compiler/operator_expressions"
|
require_relative "compiler/operator_expressions"
|
||||||
require_relative "compiler/return_expression"
|
require_relative "compiler/return_expression"
|
||||||
require_relative "compiler/while_expression"
|
require_relative "compiler/while_expression"
|
||||||
require_relative "compiler/expression_list"
|
|
||||||
|
16
lib/bosl/compiler/field_def.rb
Normal file
16
lib/bosl/compiler/field_def.rb
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
module Bosl
|
||||||
|
Compiler.class_eval do
|
||||||
|
|
||||||
|
def on_field_def expression
|
||||||
|
type , name , value = *expression
|
||||||
|
name = name
|
||||||
|
index = method.ensure_local( name )
|
||||||
|
|
||||||
|
if value
|
||||||
|
value = process( value )
|
||||||
|
end
|
||||||
|
|
||||||
|
Virtual::Return.new( value )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -2,10 +2,11 @@ module Bosl
|
|||||||
Compiler.class_eval do
|
Compiler.class_eval do
|
||||||
# function attr_reader :name, :params, :body , :receiver
|
# function attr_reader :name, :params, :body , :receiver
|
||||||
def on_function expression
|
def on_function expression
|
||||||
|
# puts expression.inspect
|
||||||
return_type , name , parameters, kids = *expression
|
return_type , name , parameters, kids = *expression
|
||||||
name = name.to_a.first
|
name = name.to_a.first
|
||||||
args = parameters.to_a.collect do |p|
|
args = parameters.to_a.collect do |p|
|
||||||
raise "error, argument must be a identifier, not #{p}" unless p.type == :field
|
raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter
|
||||||
p[2]
|
p[2]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
require_relative "test_foo"
|
require_relative "test_foo"
|
||||||
require_relative "test_if"
|
require_relative "test_if"
|
||||||
#require_relative "test_functions"
|
require_relative "test_functions"
|
||||||
#require_relative "test_string_class"
|
#require_relative "test_string_class"
|
||||||
#require_relative "test_hello"
|
#require_relative "test_hello"
|
||||||
#require_relative "test_putint"
|
#require_relative "test_putint"
|
||||||
|
@ -5,23 +5,26 @@ class TestFunctions < MiniTest::Test
|
|||||||
|
|
||||||
def test_functions
|
def test_functions
|
||||||
@string_input = <<HERE
|
@string_input = <<HERE
|
||||||
def minus(a,b)
|
int minus(int a,int b)
|
||||||
a - b
|
return a - b
|
||||||
end
|
end
|
||||||
def plus(a, b)
|
|
||||||
a + b
|
int plus(int a, int b)
|
||||||
|
return a + b
|
||||||
end
|
end
|
||||||
def times(a, b)
|
|
||||||
|
int times(int a, int b)
|
||||||
if( b == 0 )
|
if( b == 0 )
|
||||||
a = 0
|
a = 0
|
||||||
else
|
else
|
||||||
m = minus(b, 1)
|
int m = minus(b, 1)
|
||||||
t = times(a, m)
|
int t = times(a, m)
|
||||||
a = plus(a,t)
|
a = plus(a,t)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def t_seven()
|
|
||||||
tim = times(7,6)
|
int t_seven()
|
||||||
|
int tim = times(7,6)
|
||||||
tim.putint()
|
tim.putint()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user