adds basic math operators, thanks to kasper associative and prioritised
This commit is contained in:
@ -41,8 +41,8 @@ module Ast
|
||||
def initialize str
|
||||
@string = str
|
||||
end
|
||||
def inspectt
|
||||
"#{string}"
|
||||
def inspect
|
||||
self.class.name + '.new("' + string + '")'
|
||||
end
|
||||
|
||||
def compile context
|
||||
|
@ -45,4 +45,40 @@ module Ast
|
||||
[:assignee, :assigned]
|
||||
end
|
||||
end
|
||||
class OperatorExpression < Expression
|
||||
attr_reader :operator, :left, :right
|
||||
|
||||
def initialize operator, left, right
|
||||
@operator, @left, @right = operator, left, right
|
||||
end
|
||||
def attributes
|
||||
[:operator, :left, :right]
|
||||
end
|
||||
def inspect
|
||||
self.class.name + ".new(" + operator.inspect + ", " + left.inspect + "," + right.inspect + ")"
|
||||
end
|
||||
|
||||
def compile context
|
||||
parent_locals = context.locals
|
||||
context.locals = {}
|
||||
args = []
|
||||
params.each do |param|
|
||||
args << param.compile(context) # making the argument a local
|
||||
end
|
||||
# args = params.collect{|p| Vm::Value.type p.name }
|
||||
function = Vm::Function.new(name ,args )
|
||||
context.program.add_function function
|
||||
block.each do |b|
|
||||
compiled = b.compile context
|
||||
if compiled.is_a? Vm::Block
|
||||
he.breaks.loose
|
||||
else
|
||||
function.body.add_code compiled
|
||||
end
|
||||
puts compiled.inspect
|
||||
end
|
||||
context.locals = parent_locals if parent_locals
|
||||
function
|
||||
end
|
||||
end
|
||||
end
|
@ -14,5 +14,9 @@ module Parser
|
||||
rule(:expressions_else) { delimited_expressions(keyword_else) }
|
||||
rule(:expressions_end) { delimited_expressions(keyword_end) }
|
||||
|
||||
rule(:operator_expression) do
|
||||
infix_expression(expression, [multiply, 2, :left], [plus, 1, :right])
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
@ -12,7 +12,10 @@ module Parser
|
||||
rule(:comma) { str(',') >> space? }
|
||||
rule(:colon) { str(':') >> space? }
|
||||
rule(:semicolon) { str(';') >> space? }
|
||||
rule(:question_mark) { str('?') >> space? }
|
||||
rule(:excamation_mark) { str('!') >> space? }
|
||||
rule(:question_mark) { str('?') >> space? }
|
||||
rule(:excamation_mark) { str('!') >> space? }
|
||||
|
||||
rule(:multiply) { match['*/'] >> space? }
|
||||
rule(:plus) { match['+-'] >> space? }
|
||||
end
|
||||
end
|
@ -41,6 +41,11 @@ module Parser
|
||||
rule(:asignee => simple(:left) , :asigned => simple(:right) ) do
|
||||
Ast::AssignmentExpression.new(left , right )
|
||||
end
|
||||
|
||||
rule(l: simple(:l), o: simple(:o) , r: simple(:r)) do
|
||||
Ast::OperatorExpression.new( o.to_s.strip , l ,r)
|
||||
end
|
||||
|
||||
#shortcut to get the ast tree for a given string
|
||||
# optional second arguement specifies a rule that will be parsed (mainly for testing)
|
||||
def self.ast string , rule = :root
|
||||
|
Reference in New Issue
Block a user