#include #include "token.h" #include "node.h" #include "hashtable.h" #include "lexan.h" #include "world.h" node * world::parse_highest() { token t = Lex.next(); token_type tt = t.gettype(); if (tt == tnumber) return node::num(t.getvalue()); else if (tt == tvariable) return node::var(t.getstring()); else if (tt == toperator && t.getstring() == "(") { node * n = parse_expression(); t = Lex.next(); if (t.gettype() == toperator && t.getstring() == ")") return n; return node::err("error in parentheses"); } return node::err("parse_highest"); } node * world::parse_power() { node * L = parse_highest(); token t = Lex.next(); while (t.gettype() == toperator && t.getstring() == "^") { node * R = parse_highest(); L = node::op("^", L, R); t = Lex.next(); } Lex.unnext(t); return L; } node * world::parse_muldiv() { node * L = parse_power(); token t = Lex.next(); while (t.gettype() == toperator && (t.getstring() == "*" || t.getstring() == "/")) { node * R = parse_power(); L = node::op(t.getstring(), L, R); t = Lex.next(); } Lex.unnext(t); return L; } node * world::parse_addsub() { node * L = parse_muldiv(); token t = Lex.next(); while (t.gettype() == toperator && (t.getstring() == "+" || t.getstring() == "-")) { node * R = parse_muldiv(); L = node::op(t.getstring(), L, R); t = Lex.next(); } Lex.unnext(t); return L; } node * world::parse_expression() { return parse_addsub(); }