1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
grammar Arithmetic
rule expression
comparative / additive
end
rule comparative
head:additive
tail:(
space operator:equality_op
space operand:additive)* <BinaryOperation>
end
rule equality_op
'==' {
def apply(a, b)
a == b
end
}
end
rule additive
head:multitive
tail:(
space operator:additive_op
space operand:multitive)* <BinaryOperation>
end
rule additive_op
'+' {
def apply(a, b)
a + b
end
}
/
'-' {
def apply(a, b)
a - b
end
}
end
rule multitive
head:primary
tail:(
space operator:multitive_op
space operand:primary)* <BinaryOperation>
end
rule multitive_op
'*' {
def apply(a, b)
a * b
end
}
/
'/' {
def apply(a, b)
a / b
end
}
end
rule primary
variable
/
number
/
'(' space expression space ')' {
def eval(env={})
expression.eval(env)
end
}
end
rule variable
[a-z]+ {
def eval(env={})
env[name]
end
def name
text_value
end
}
end
rule number
([1-9] [0-9]* / '0') {
def eval(env={})
text_value.to_i
end
}
end
rule space
' '*
end
end
|