Skip to content

Instantly share code, notes, and snippets.

@pikajude
Created April 9, 2011 00:47
Show Gist options
  • Select an option

  • Save pikajude/910983 to your computer and use it in GitHub Desktop.

Select an option

Save pikajude/910983 to your computer and use it in GitHub Desktop.
grammar Arithmetic
rule sum_operand
mdiv_operand (sumoperator mdiv_operand)* {
def content
expr_cont
end
}
end
rule mdiv_operand
exp_operand (mdivoperator exp_operand)* {
def content
expr_cont
end
}
end
rule exp_operand
operand (expoperator operand)* {
def content
expr_cont
end
}
end
rule operand
fn_num / '(' sum_operand ')' {
def content
elements[1] ? elements[1].content : elements[0].content
end
}
end
rule fn_num
num / fnname '(' sum_operand ')' {
def content
elements[1].terminal? ? [elements[0].text_value, elements[2].content] : elements[0].content
end
}
end
rule fnname
[a-z_] [_a-z0-9]+ {
def content
text_value
end
}
end
rule num
'-'? ('0x' [0-9a-fA-F]+ / '0b' [01]+ / ([0-9]+ ('.' [0-9]+)?) / ([Pp] [Ii]) / 'π' / [Ee] / [Ii]) {
def content
if %w[pi e i π].include?(text_value.downcase)
if "i" == text_value.downcase
return Complex(0, 1)
else
return Math.const_get((text_value == 'π' ? 'pi' : text_value).upcase.to_sym)
end
end
case text_value[0...2]
when "0."
text_value.to_f
when /0./
Integer(text_value).to_f
else
text_value.to_f
end
end
}
end
rule mdivoperator
[\/\*] {
def content
text_value.to_sym
end
}
end
rule sumoperator
[+\-] {
def content
text_value.to_sym
end
}
end
rule expoperator
[\^] {
def content
text_value.to_sym
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment