EOI: Lecture 14

This commit is contained in:
Edward R. Gonzalez 2022-07-24 21:47:00 -04:00
parent 88644a95df
commit 9bc4b9e8de
7 changed files with 140 additions and 86 deletions

18
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "GDScript Godot",
"type": "godot",
"request": "launch",
"project": "${workspaceFolder}/App",
"port": 6007,
"address": "127.0.0.1",
"launch_game_instance": true,
"launch_scene": false
}
]
}

View File

@ -1,3 +1,4 @@
{
"editor.formatOnType": true
"editor.formatOnType": true,
"godot_tools.editor_path": "p:\\Study\\LangStudies\\Engine\\gd\\bin\\godot.windows.opt.tools.64.exe"
}

View File

@ -67,6 +67,16 @@ func eval( ast ):
if ast.num_args() > 2:
return eval( ast.arg(3))
NType.expr_Switch:
var index = 1
while ast.arg(index).is_op_Relation():
if eval( ast.arg(index) ):
return eval( ast.arg(index + 1) )
index += 2
return eval( ast.arg(index) )
NType.expr_While :
var result
@ -75,6 +85,20 @@ func eval( ast ):
return result
NType.expr_For:
var forEva = get_script().new( self, EvalOut )
forEva.eval( ast.arg(1) )
var index = 3; var result
while forEva.eval( ast.arg(2) ) :
result = forEva.eval( ast.arg(index) )
index += 1
if index > ast.num_args() :
index = 3
return result
NType.fn_User :
var symbol = ast.arg(1)
var fnDef = \
@ -120,13 +144,55 @@ func eval( ast ):
NType.identifier :
return eval_Lookup( ast )
NType.op_Assign :
return eval_Assign( ast )
NType.op_Fn:
return eval_Func( ast )
NType.op_Add:
var result = 0.0; var index = 1
while index <= ast.num_args():
result += eval( ast.arg(index) )
index += 1
return result
NType.op_Sub:
if ast.num_args() < 2:
return -eval( ast.arg(1) )
var result = eval( ast.arg(1) ); var index = 2
while index <= ast.num_args():
result -= eval( ast.arg(index) )
index += 1
return result
NType.op_Mult:
var result = 1.0; var index = 1
while index <= ast.num_args():
result *= eval( ast.arg(index) )
index += 1
return result
NType.op_Div:
var result = eval( ast.arg(1) ); var index = 2
while index <= ast.num_args():
result /= eval( ast.arg(index) )
index += 1
return result
NType.op_Increment:
return eval( ast.arg(1) ) + 1
NType.op_Decrement:
return eval( ast.arg(1) ) - 1
NType.op_Greater:
return eval( ast.arg(1) ) > eval( ast.arg(2) )
NType.op_Lesser:
@ -161,7 +227,9 @@ func eval( ast ):
elif ast.is_String() :
return ast.string()
return eval_Numeric( ast )
var msgT = "eval - Unimplemented: {ast}"
var msg = msgT.format({"ast" : JSON.print(ast.to_SExpression(), "\t") })
throw(msg)
func eval_Block( ast ):
var eva_Block = get_script().new( self, EvalOut )
@ -220,50 +288,6 @@ func eval_Func( ast ):
return result
func eval_Numeric( ast ):
if ast.type() == NType.op_Add:
var result = 0.0; var index = 1
while index <= ast.num_args():
result += eval( ast.arg(index) )
index += 1
return result
if ast.type() == NType.op_Sub:
if ast.num_args() < 2:
return -eval( ast.arg(1) )
var result = eval( ast.arg(1) ); var index = 2
while index <= ast.num_args():
result -= eval( ast.arg(index) )
index += 1
return result
if ast.type() == NType.op_Mult:
var result = 1.0; var index = 1
while index <= ast.num_args():
result *= eval( ast.arg(index) )
index += 1
return result
if ast.type() == NType.op_Div:
var result = eval( ast.arg(1) ); var index = 2
while index <= ast.num_args():
result /= eval( ast.arg(index) )
index += 1
return result
var msgT = "eval - Unimplemented: {ast}"
var msg = msgT.format({"ast" : JSON.print(ast.to_SExpression(), "\t") })
throw(msg)
func eval_Print( ast ):
EvalOut.text += "\n" + String( eval( ast.arg(1) ) )
return null

View File

@ -28,7 +28,9 @@ const TType : Dictionary = \
def_End = "Expression End",
def_Block = "Expression Block Start",
def_Cond = "Expression Conditional",
def_Switch = "Expresssion Switch",
def_While = "Expression While",
def_For = "Expression For",
def_Var = "Variable Declaration",
def_Func = "Function Declaration",
def_Lambda = "Lambda Declaration",
@ -57,7 +59,9 @@ const Spec : Dictionary = \
TType.def_End : "start \\)",
TType.def_Block : "start \"begin\"",
TType.def_Cond : "start \"if\"",
TType.def_Switch : "start \"switch\"",
TType.def_While : "start \"while\"",
TType.def_For : "start \"for\"",
TType.def_Var : "start \"var\"",
TType.def_Func : "start \"def\"",
TType.def_Lambda : "start \"lambda\"",
@ -71,7 +75,7 @@ const Spec : Dictionary = \
TType.literal_String : "start \\\" !set( \\\" ).repeat(0-) \\\" ",
TType.op_Assgin : "start \"set\"",
TType.op_Numeric : "start set(+ \\- * /)",
TType.op_Numeric : "start set(+ \\- * /) set(+ \\-).repeat(0-1)",
TType.op_Relational : "start set(> <) =.repeat(0-1)",
TType.op_Equality : "start \\!.repeat(0-1) =",

View File

@ -26,7 +26,9 @@ const NType = \
block = "Scope Block",
conditional = "Conditional",
expr_Switch = "Expression Switch",
expr_While = "Expression While",
expr_For = "Expression For",
literal_Number = "Literal: Number",
literal_String = "Literal: String",
@ -34,10 +36,12 @@ const NType = \
op_Assign = "Assignment",
op_Fn = "Function Call",
op_Add = "+",
op_Sub = "-",
op_Mult = "*",
op_Div = "/",
op_Add = "+",
op_Sub = "-",
op_Mult = "*",
op_Div = "/",
op_Increment = "++",
op_Decrement = "--",
op_Greater = ">",
op_GreaterEqual = ">=",
@ -63,7 +67,6 @@ class ASTNode:
func get_class() :
return "ASTNode"
func add_Expr( expr ):
Data.append(expr)
@ -82,12 +85,24 @@ class ASTNode:
func type():
return Data[0]
func is_op_Relation():
match type():
NType.op_Greater: return true
NType.op_Lesser: return true
NType.op_GreaterEqual: return true
NType.op_LesserEqual: return true
NType.op_Equal: return true
NType.op_NotEqual: return true
_: return false
func is_op_Numeric():
match type():
NType.op_Add: return true
NType.op_Sub: return true
NType.op_Mult: return true
NType.op_Div: return true
NType.op_Increment: return true
NType.op_Decrement: return true
_: return false
func is_Number():
@ -173,11 +188,15 @@ func parse_Expression():
match NextToken.Type :
TType.def_Block:
node = parse_Block()
node = parse_Simple(TType.def_Block, NType.block)
TType.def_Cond:
node = parse_ConditionalIf()
node = parse_Simple(TType.def_cond, NType.conditional)
TType.def_Switch:
node = parse_Switch()
TType.def_While:
node = parse_While()
node = parse_Simple(TType.def_While, NType.expr_While)
TType.def_For:
node = parse_Simple(TType.def_For, NType.expr_For)
TType.def_Var:
node = parse_Variable()
TType.def_Func:
@ -185,7 +204,7 @@ func parse_Expression():
TType.def_Lambda:
node = parse_fn_Lambda()
TType.fn_Print:
node = parse_fn_Print()
node = parse_Simple(TType.fn_Print, NType.fn_Print)
TType.op_Assgin:
node = parse_op_Assign()
TType.op_Numeric:
@ -205,7 +224,6 @@ func parse_Expression():
TType.def_Start:
node = parse_fn_IIL()
var arg = 1
while NextToken.Type != TType.def_End:
if NextToken.Type == TType.def_Start:
node.add_Expr( parse_Expression() )
@ -222,26 +240,20 @@ func parse_Expression():
return node
func parse_Block():
func parse_Simple(tType, nType):
var \
node = ASTNode.new()
node.set_Type(NType.block)
eat(TType.def_Block)
node.set_Type(nType)
eat(tType)
return node
func parse_ConditionalIf():
func parse_Switch():
var \
node = ASTNode.new()
node.set_Type(NType.conditional)
eat(TType.def_Cond)
return node
node.set_Type(NType.expr_Switch)
eat(TType.def_Switch)
func parse_While():
var \
node = ASTNode.new()
node.set_Type(NType.expr_While)
eat(TType.def_While)
return node
func parse_Variable():
@ -358,15 +370,6 @@ func parse_Identifier():
return node
func parse_fn_Print():
var \
node = ASTNode.new()
node.set_Type(NType.fn_Print)
eat(TType.fn_Print)
return node
func parse_op_Assign():
var \
node = ASTNode.new()
@ -402,6 +405,10 @@ func parse_op_Numeric():
node.set_Type(NType.op_Mult)
NType.op_Div:
node.set_Type(NType.op_Div)
NType.op_Increment:
node.set_Type(NType.op_Increment)
NType.op_Decrement:
node.set_Type(NType.op_Decrement)
eat(TType.op_Numeric)

@ -1 +1 @@
Subproject commit 32ef964b0f7d4e18a919e904988727b3ed775901
Subproject commit 270f5a3bd1aa3d2f98eeaaae9e36085c9b7df8ef