Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -3,60 +3,60 @@ Expr.eval = 0
|
|||
|
||||
BinaryExpr = new Expr
|
||||
BinaryExpr.eval = function()
|
||||
if self.op == "+" then return self.lhs.eval + self.rhs.eval
|
||||
if self.op == "-" then return self.lhs.eval - self.rhs.eval
|
||||
if self.op == "*" then return self.lhs.eval * self.rhs.eval
|
||||
if self.op == "/" then return self.lhs.eval / self.rhs.eval
|
||||
if self.op == "+" then return self.lhs.eval + self.rhs.eval
|
||||
if self.op == "-" then return self.lhs.eval - self.rhs.eval
|
||||
if self.op == "*" then return self.lhs.eval * self.rhs.eval
|
||||
if self.op == "/" then return self.lhs.eval / self.rhs.eval
|
||||
end function
|
||||
binop = function(lhs, op, rhs)
|
||||
e = new BinaryExpr
|
||||
e.lhs = lhs
|
||||
e.op = op
|
||||
e.rhs = rhs
|
||||
return e
|
||||
e = new BinaryExpr
|
||||
e.lhs = lhs
|
||||
e.op = op
|
||||
e.rhs = rhs
|
||||
return e
|
||||
end function
|
||||
|
||||
parseAtom = function(inp)
|
||||
tok = inp.pull
|
||||
if tok >= "0" and tok <= "9" then
|
||||
e = new Expr
|
||||
e.eval = val(tok)
|
||||
while inp and inp[0] >= "0" and inp[0] <= "9"
|
||||
e.eval = e.eval * 10 + val(inp.pull)
|
||||
end while
|
||||
else if tok == "(" then
|
||||
e = parseAddSub(inp)
|
||||
inp.pull // swallow closing ")"
|
||||
return e
|
||||
else
|
||||
print "Unexpected token: " + tok
|
||||
exit
|
||||
end if
|
||||
return e
|
||||
tok = inp.pull
|
||||
if tok >= "0" and tok <= "9" then
|
||||
e = new Expr
|
||||
e.eval = val(tok)
|
||||
while inp and inp[0] >= "0" and inp[0] <= "9"
|
||||
e.eval = e.eval * 10 + val(inp.pull)
|
||||
end while
|
||||
else if tok == "(" then
|
||||
e = parseAddSub(inp)
|
||||
inp.pull // swallow closing ")"
|
||||
return e
|
||||
else
|
||||
print "Unexpected token: " + tok
|
||||
exit
|
||||
end if
|
||||
return e
|
||||
end function
|
||||
|
||||
parseMultDiv = function(inp)
|
||||
next = @parseAtom
|
||||
e = next(inp)
|
||||
while inp and (inp[0] == "*" or inp[0] == "/")
|
||||
e = binop(e, inp.pull, next(inp))
|
||||
end while
|
||||
return e
|
||||
next = @parseAtom
|
||||
e = next(inp)
|
||||
while inp and (inp[0] == "*" or inp[0] == "/")
|
||||
e = binop(e, inp.pull, next(inp))
|
||||
end while
|
||||
return e
|
||||
end function
|
||||
|
||||
parseAddSub = function(inp)
|
||||
next = @parseMultDiv
|
||||
e = next(inp)
|
||||
while inp and (inp[0] == "+" or inp[0] == "-")
|
||||
e = binop(e, inp.pull, next(inp))
|
||||
end while
|
||||
return e
|
||||
next = @parseMultDiv
|
||||
e = next(inp)
|
||||
while inp and (inp[0] == "+" or inp[0] == "-")
|
||||
e = binop(e, inp.pull, next(inp))
|
||||
end while
|
||||
return e
|
||||
end function
|
||||
|
||||
while true
|
||||
s = input("Enter expression: ").replace(" ","")
|
||||
if not s then break
|
||||
inp = split(s, "")
|
||||
ast = parseAddSub(inp)
|
||||
print ast.eval
|
||||
s = input("Enter expression: ").replace(" ","")
|
||||
if not s then break
|
||||
inp = split(s, "")
|
||||
ast = parseAddSub(inp)
|
||||
print ast.eval
|
||||
end while
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue