EOI: Lecture 13 complete

This commit is contained in:
Edward R. Gonzalez 2022-07-24 10:33:26 -04:00
parent a6c99fcfa6
commit 88644a95df
3 changed files with 23 additions and 6 deletions

View File

@ -136,6 +136,11 @@ func eval( ast ):
NType.op_LesserEqual: NType.op_LesserEqual:
return eval( ast.arg(1) ) <= eval( ast.arg(2) ) return eval( ast.arg(1) ) <= eval( ast.arg(2) )
NType.op_Equal:
return eval( ast.arg(1) ) == eval( ast.arg(2) )
NType.op_NotEqual:
return eval( ast.arg(1) ) != eval( ast.arg(2) )
NType.fn_Print : NType.fn_Print :
return eval_Print( ast ) return eval_Print( ast )
@ -226,10 +231,10 @@ func eval_Numeric( ast ):
return result return result
if ast.type() == NType.op_Sub: if ast.type() == NType.op_Sub:
if ast.num_args() == 2: if ast.num_args() < 2:
return -eval( ast.arg(1) ) return -eval( ast.arg(1) )
var result = 0.0; var index = 1 var result = eval( ast.arg(1) ); var index = 2
while index <= ast.num_args(): while index <= ast.num_args():
result -= eval( ast.arg(index) ) result -= eval( ast.arg(index) )
@ -247,11 +252,11 @@ func eval_Numeric( ast ):
return result return result
if ast.type() == NType.op_Div: if ast.type() == NType.op_Div:
var result = 1.0; var index = 1 var result = eval( ast.arg(1) ); var index = 2
while index <= ast.num_args(): while index <= ast.num_args():
result /= eval( ast.arg(index) ) result /= eval( ast.arg(index) )
result += 1 index += 1
return result return result

View File

@ -37,8 +37,9 @@ const TType : Dictionary = \
literal_String = "Literal: String", literal_String = "Literal: String",
op_Assgin = "Assignment", op_Assgin = "Assignment",
op_Numeric = "op_Numeric", op_Numeric = "Numeric Operation",
op_Relational = "op_Relational", op_Relational = "Relational Operation",
op_Equality = "Equality Operation",
fn_Print = "Print", fn_Print = "Print",
@ -72,6 +73,7 @@ const Spec : Dictionary = \
TType.op_Assgin : "start \"set\"", TType.op_Assgin : "start \"set\"",
TType.op_Numeric : "start set(+ \\- * /)", TType.op_Numeric : "start set(+ \\- * /)",
TType.op_Relational : "start set(> <) =.repeat(0-1)", TType.op_Relational : "start set(> <) =.repeat(0-1)",
TType.op_Equality : "start \\!.repeat(0-1) =",
TType.fn_Print : "start \"print\"", TType.fn_Print : "start \"print\"",

View File

@ -43,6 +43,8 @@ const NType = \
op_GreaterEqual = ">=", op_GreaterEqual = ">=",
op_Lesser = "<", op_Lesser = "<",
op_LesserEqual = "<=", op_LesserEqual = "<=",
op_Equal = "=",
op_NotEqual = "!=",
fn_Print = "Print", fn_Print = "Print",
fn_User = "User Function", fn_User = "User Function",
@ -190,6 +192,14 @@ func parse_Expression():
node = parse_op_Numeric() node = parse_op_Numeric()
TType.op_Relational: TType.op_Relational:
node = parse_op_Relational() node = parse_op_Relational()
TType.op_Equality:
node = ASTNode.new()
match NextToken.Value:
NType.op_Equal:
node.set_Type(NType.op_Equal)
NType.op_NotEqual:
node.set_Type(NType.op_NotEqual)
eat(TType.op_Equality)
TType.identifier: TType.identifier:
node = parse_op_Fn() node = parse_op_Fn()
TType.def_Start: TType.def_Start: