mirror of
https://github.com/CHOMPStation2/CHOMPStation2.git
synced 2026-08-25 06:07:22 +01:00
[MIRROR] Staticpath [MDB ignore] (#12445)
Co-authored-by: Kashargul <144968721+Kashargul@users.noreply.github.com>
This commit is contained in:
co-authored by
Kashargul
parent
cc106f8032
commit
ba5224fbcb
@@ -1,24 +1,24 @@
|
||||
/proc/isobject(x)
|
||||
return !(isnum(x) || istext(x))
|
||||
|
||||
/n_Interpreter/proc/Eval(node/expression/exp)
|
||||
if(istype(exp, /node/expression/FunctionCall))
|
||||
/datum/n_Interpreter/proc/Eval(datum/node/expression/exp)
|
||||
if(istype(exp, /datum/node/expression/FunctionCall))
|
||||
return RunFunction(exp)
|
||||
else if(istype(exp, /node/expression/op))
|
||||
else if(istype(exp, /datum/node/expression/op))
|
||||
return EvalOperator(exp)
|
||||
else if(istype(exp, /node/expression/value/literal))
|
||||
var/node/expression/value/literal/lit=exp
|
||||
else if(istype(exp, /datum/node/expression/value/literal))
|
||||
var/datum/node/expression/value/literal/lit=exp
|
||||
return lit.value
|
||||
else if(istype(exp, /node/expression/value/reference))
|
||||
var/node/expression/value/reference/ref=exp
|
||||
else if(istype(exp, /datum/node/expression/value/reference))
|
||||
var/datum/node/expression/value/reference/ref=exp
|
||||
return ref.value
|
||||
else if(istype(exp, /node/expression/value/variable))
|
||||
var/node/expression/value/variable/v=exp
|
||||
else if(istype(exp, /datum/node/expression/value/variable))
|
||||
var/datum/node/expression/value/variable/v=exp
|
||||
if(!v.object)
|
||||
return Eval(GetVariable(v.id.id_name))
|
||||
else
|
||||
var/datum/D
|
||||
if(istype(v.object, /node/identifier))
|
||||
if(istype(v.object, /datum/node/identifier))
|
||||
D=GetVariable(v.object:id_name)
|
||||
else
|
||||
D=v.object
|
||||
@@ -26,142 +26,142 @@
|
||||
if(!isobject(D))
|
||||
return null
|
||||
if(!D.vars.Find(v.id.id_name))
|
||||
RaiseError(new/runtimeError/UndefinedVariable("[v.object.ToString()].[v.id.id_name]"))
|
||||
RaiseError(new/datum/runtimeError/UndefinedVariable("[v.object.ToString()].[v.id.id_name]"))
|
||||
return null
|
||||
return Eval(D.vars[v.id.id_name])
|
||||
else if(istype(exp, /node/expression))
|
||||
RaiseError(new/runtimeError/UnknownInstruction())
|
||||
else if(istype(exp, /datum/node/expression))
|
||||
RaiseError(new/datum/runtimeError/UnknownInstruction())
|
||||
else
|
||||
return exp
|
||||
|
||||
/n_Interpreter/proc/EvalOperator(node/expression/op/exp)
|
||||
if(istype(exp, /node/expression/op/binary))
|
||||
var/node/expression/op/binary/bin=exp
|
||||
/datum/n_Interpreter/proc/EvalOperator(datum/node/expression/op/exp)
|
||||
if(istype(exp, /datum/node/expression/op/binary))
|
||||
var/datum/node/expression/op/binary/bin=exp
|
||||
switch(bin.type)
|
||||
if(/node/expression/op/binary/Equal)
|
||||
if(/datum/node/expression/op/binary/Equal)
|
||||
return Equal(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/NotEqual)
|
||||
if(/datum/node/expression/op/binary/NotEqual)
|
||||
return NotEqual(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/Greater)
|
||||
if(/datum/node/expression/op/binary/Greater)
|
||||
return Greater(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/Less)
|
||||
if(/datum/node/expression/op/binary/Less)
|
||||
return Less(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/GreaterOrEqual)
|
||||
if(/datum/node/expression/op/binary/GreaterOrEqual)
|
||||
return GreaterOrEqual(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/LessOrEqual)
|
||||
if(/datum/node/expression/op/binary/LessOrEqual)
|
||||
return LessOrEqual(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/LogicalAnd)
|
||||
if(/datum/node/expression/op/binary/LogicalAnd)
|
||||
return LogicalAnd(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/LogicalOr)
|
||||
if(/datum/node/expression/op/binary/LogicalOr)
|
||||
return LogicalOr(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/LogicalXor)
|
||||
if(/datum/node/expression/op/binary/LogicalXor)
|
||||
return LogicalXor(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/BitwiseAnd)
|
||||
if(/datum/node/expression/op/binary/BitwiseAnd)
|
||||
return BitwiseAnd(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/BitwiseOr)
|
||||
if(/datum/node/expression/op/binary/BitwiseOr)
|
||||
return BitwiseOr(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/BitwiseXor)
|
||||
if(/datum/node/expression/op/binary/BitwiseXor)
|
||||
return BitwiseXor(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/Add)
|
||||
if(/datum/node/expression/op/binary/Add)
|
||||
return Add(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/Subtract)
|
||||
if(/datum/node/expression/op/binary/Subtract)
|
||||
return Subtract(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/Multiply)
|
||||
if(/datum/node/expression/op/binary/Multiply)
|
||||
return Multiply(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/Divide)
|
||||
if(/datum/node/expression/op/binary/Divide)
|
||||
return Divide(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/Power)
|
||||
if(/datum/node/expression/op/binary/Power)
|
||||
return Power(Eval(bin.exp), Eval(bin.exp2))
|
||||
if(/node/expression/op/binary/Modulo)
|
||||
if(/datum/node/expression/op/binary/Modulo)
|
||||
return Modulo(Eval(bin.exp), Eval(bin.exp2))
|
||||
else
|
||||
RaiseError(new/runtimeError/UnknownInstruction())
|
||||
RaiseError(new/datum/runtimeError/UnknownInstruction())
|
||||
return
|
||||
switch(exp.type)
|
||||
if(/node/expression/op/unary/Minus)
|
||||
if(/datum/node/expression/op/unary/Minus)
|
||||
return Minus(Eval(exp.exp))
|
||||
if(/node/expression/op/unary/LogicalNot)
|
||||
if(/datum/node/expression/op/unary/LogicalNot)
|
||||
return LogicalNot(Eval(exp.exp))
|
||||
if(/node/expression/op/unary/BitwiseNot)
|
||||
if(/datum/node/expression/op/unary/BitwiseNot)
|
||||
return BitwiseNot(Eval(exp.exp))
|
||||
if(/node/expression/op/unary/group)
|
||||
if(/datum/node/expression/op/unary/group)
|
||||
return Eval(exp.exp)
|
||||
else
|
||||
RaiseError(new/runtimeError/UnknownInstruction())
|
||||
RaiseError(new/datum/runtimeError/UnknownInstruction())
|
||||
|
||||
|
||||
//Binary//
|
||||
//Comparison operators
|
||||
/n_Interpreter/proc/Equal(a, b) return a==b
|
||||
/n_Interpreter/proc/NotEqual(a, b) return a!=b //LogicalNot(Equal(a, b))
|
||||
/n_Interpreter/proc/Greater(a, b) return a>b
|
||||
/n_Interpreter/proc/Less(a, b) return a<b
|
||||
/n_Interpreter/proc/GreaterOrEqual(a, b) return a>=b
|
||||
/n_Interpreter/proc/LessOrEqual(a, b) return a<=b
|
||||
/datum/n_Interpreter/proc/Equal(a, b) return a==b
|
||||
/datum/n_Interpreter/proc/NotEqual(a, b) return a!=b //LogicalNot(Equal(a, b))
|
||||
/datum/n_Interpreter/proc/Greater(a, b) return a>b
|
||||
/datum/n_Interpreter/proc/Less(a, b) return a<b
|
||||
/datum/n_Interpreter/proc/GreaterOrEqual(a, b) return a>=b
|
||||
/datum/n_Interpreter/proc/LessOrEqual(a, b) return a<=b
|
||||
//Logical Operators
|
||||
/n_Interpreter/proc/LogicalAnd(a, b) return a&&b
|
||||
/n_Interpreter/proc/LogicalOr(a, b) return a||b
|
||||
/n_Interpreter/proc/LogicalXor(a, b) return (a||b) && !(a&&b)
|
||||
/datum/n_Interpreter/proc/LogicalAnd(a, b) return a&&b
|
||||
/datum/n_Interpreter/proc/LogicalOr(a, b) return a||b
|
||||
/datum/n_Interpreter/proc/LogicalXor(a, b) return (a||b) && !(a&&b)
|
||||
//Bitwise Operators
|
||||
/n_Interpreter/proc/BitwiseAnd(a, b) return a&b
|
||||
/n_Interpreter/proc/BitwiseOr(a, b) return a|b
|
||||
/n_Interpreter/proc/BitwiseXor(a, b) return a^b
|
||||
/datum/n_Interpreter/proc/BitwiseAnd(a, b) return a&b
|
||||
/datum/n_Interpreter/proc/BitwiseOr(a, b) return a|b
|
||||
/datum/n_Interpreter/proc/BitwiseXor(a, b) return a^b
|
||||
//Arithmetic Operators
|
||||
/n_Interpreter/proc/Add(a, b)
|
||||
/datum/n_Interpreter/proc/Add(a, b)
|
||||
if(istext(a)&&!istext(b)) b="[b]"
|
||||
else if(istext(b)&&!istext(a)) a="[a]"
|
||||
if(isobject(a) && !isobject(b))
|
||||
RaiseError(new/runtimeError/TypeMismatch("+", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("+", a, b))
|
||||
return null
|
||||
else if(isobject(b) && !isobject(a))
|
||||
RaiseError(new/runtimeError/TypeMismatch("+", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("+", a, b))
|
||||
return null
|
||||
return a+b
|
||||
/n_Interpreter/proc/Subtract(a, b)
|
||||
/datum/n_Interpreter/proc/Subtract(a, b)
|
||||
if(isobject(a) && !isobject(b))
|
||||
RaiseError(new/runtimeError/TypeMismatch("-", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("-", a, b))
|
||||
return null
|
||||
else if(isobject(b) && !isobject(a))
|
||||
RaiseError(new/runtimeError/TypeMismatch("-", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("-", a, b))
|
||||
return null
|
||||
return a-b
|
||||
/n_Interpreter/proc/Divide(a, b)
|
||||
/datum/n_Interpreter/proc/Divide(a, b)
|
||||
if(isobject(a) && !isobject(b))
|
||||
RaiseError(new/runtimeError/TypeMismatch("/", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("/", a, b))
|
||||
return null
|
||||
else if(isobject(b) && !isobject(a))
|
||||
RaiseError(new/runtimeError/TypeMismatch("/", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("/", a, b))
|
||||
return null
|
||||
if(b==0)
|
||||
RaiseError(new/runtimeError/DivisionByZero())
|
||||
RaiseError(new/datum/runtimeError/DivisionByZero())
|
||||
return null
|
||||
return a/b
|
||||
/n_Interpreter/proc/Multiply(a, b)
|
||||
/datum/n_Interpreter/proc/Multiply(a, b)
|
||||
if(isobject(a) && !isobject(b))
|
||||
RaiseError(new/runtimeError/TypeMismatch("*", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("*", a, b))
|
||||
return null
|
||||
else if(isobject(b) && !isobject(a))
|
||||
RaiseError(new/runtimeError/TypeMismatch("*", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("*", a, b))
|
||||
return null
|
||||
return a*b
|
||||
/n_Interpreter/proc/Modulo(a, b)
|
||||
/datum/n_Interpreter/proc/Modulo(a, b)
|
||||
if(isobject(a) && !isobject(b))
|
||||
RaiseError(new/runtimeError/TypeMismatch("%", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("%", a, b))
|
||||
return null
|
||||
else if(isobject(b) && !isobject(a))
|
||||
RaiseError(new/runtimeError/TypeMismatch("%", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("%", a, b))
|
||||
return null
|
||||
return a%b
|
||||
/n_Interpreter/proc/Power(a, b)
|
||||
/datum/n_Interpreter/proc/Power(a, b)
|
||||
if(isobject(a) && !isobject(b))
|
||||
RaiseError(new/runtimeError/TypeMismatch("**", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("**", a, b))
|
||||
return null
|
||||
else if(isobject(b) && !isobject(a))
|
||||
RaiseError(new/runtimeError/TypeMismatch("**", a, b))
|
||||
RaiseError(new/datum/runtimeError/TypeMismatch("**", a, b))
|
||||
return null
|
||||
return a**b
|
||||
|
||||
//Unary//
|
||||
/n_Interpreter/proc/Minus(a) return -a
|
||||
/n_Interpreter/proc/LogicalNot(a) return !a
|
||||
/n_Interpreter/proc/BitwiseNot(a) return ~a
|
||||
/datum/n_Interpreter/proc/Minus(a) return -a
|
||||
/datum/n_Interpreter/proc/LogicalNot(a) return !a
|
||||
/datum/n_Interpreter/proc/BitwiseNot(a) return ~a
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
Class: n_Interpreter
|
||||
Procedures allowing for interaction with the script that is being run by the interpreter object.
|
||||
*/
|
||||
/n_Interpreter
|
||||
/datum/n_Interpreter
|
||||
|
||||
/*
|
||||
Proc: Load
|
||||
@@ -15,7 +15,7 @@
|
||||
Parameters:
|
||||
program - A <GlobalBlock> object which represents the script's global scope.
|
||||
*/
|
||||
/n_Interpreter/proc/Load(node/BlockDefinition/GlobalBlock/program)
|
||||
/datum/n_Interpreter/proc/Load(datum/node/BlockDefinition/GlobalBlock/program)
|
||||
ASSERT(program)
|
||||
src.program = program
|
||||
CreateGlobalScope()
|
||||
@@ -24,7 +24,7 @@
|
||||
Proc: Run
|
||||
Runs the script.
|
||||
*/
|
||||
/n_Interpreter/proc/Run()
|
||||
/datum/n_Interpreter/proc/Run()
|
||||
cur_recursion = 0 // reset recursion
|
||||
cur_statements = 0 // reset CPU tracking
|
||||
alertadmins = 0
|
||||
@@ -43,7 +43,7 @@
|
||||
See Also:
|
||||
- <Block.SetVar()>
|
||||
*/
|
||||
/n_Interpreter/proc/SetVar(name, value)
|
||||
/datum/n_Interpreter/proc/SetVar(name, value)
|
||||
if(!istext(name))
|
||||
//CRASH("Invalid variable name")
|
||||
return
|
||||
@@ -59,24 +59,24 @@
|
||||
object - (Optional) An object which will the be target of a function call.
|
||||
params - Only required if object is not null, a list of the names of parameters the proc takes.
|
||||
*/
|
||||
/n_Interpreter/proc/SetProc(name, path, object=null, list/params=null)
|
||||
/datum/n_Interpreter/proc/SetProc(name, path, object=null, list/params=null)
|
||||
if(!istext(name))
|
||||
//CRASH("Invalid function name")
|
||||
return
|
||||
if(!object)
|
||||
globalScope.functions[name] = path
|
||||
return
|
||||
var/node/statement/FunctionDefinition/S = new()
|
||||
var/datum/node/statement/FunctionDefinition/S = new()
|
||||
S.func_name = name
|
||||
S.parameters = params
|
||||
S.block = new()
|
||||
S.block.SetVar("src", object)
|
||||
var/node/expression/FunctionCall/C = new()
|
||||
var/datum/node/expression/FunctionCall/C = new()
|
||||
C.func_name = path
|
||||
C.object = new("src")
|
||||
for(var/p in params)
|
||||
C.parameters += new/node/expression/value/variable(p)
|
||||
var/node/statement/ReturnStatement/R=new()
|
||||
C.parameters += new/datum/node/expression/value/variable(p)
|
||||
var/datum/node/statement/ReturnStatement/R=new()
|
||||
R.value=C
|
||||
S.block.statements += R
|
||||
globalScope.functions[name] = S
|
||||
@@ -84,14 +84,14 @@
|
||||
Proc: VarExists
|
||||
Checks whether a global variable with the specified name exists.
|
||||
*/
|
||||
/n_Interpreter/proc/VarExists(name)
|
||||
/datum/n_Interpreter/proc/VarExists(name)
|
||||
return globalScope.variables.Find(name) //convert to 1/0 first?
|
||||
|
||||
/*
|
||||
Proc: ProcExists
|
||||
Checks whether a global function with the specified name exists.
|
||||
*/
|
||||
/n_Interpreter/proc/ProcExists(name)
|
||||
/datum/n_Interpreter/proc/ProcExists(name)
|
||||
return globalScope.functions.Find(name)
|
||||
|
||||
/*
|
||||
@@ -101,7 +101,7 @@
|
||||
See Also:
|
||||
- <VarExists()>
|
||||
*/
|
||||
/n_Interpreter/proc/GetVar(name)
|
||||
/datum/n_Interpreter/proc/GetVar(name)
|
||||
if(!VarExists(name))
|
||||
//CRASH("No variable named '[name]'.")
|
||||
return
|
||||
@@ -116,13 +116,13 @@
|
||||
See Also:
|
||||
- <ProcExists()>
|
||||
*/
|
||||
/n_Interpreter/proc/CallProc(name, params[]=null)
|
||||
/datum/n_Interpreter/proc/CallProc(name, params[]=null)
|
||||
if(!ProcExists(name))
|
||||
//CRASH("No function named '[name]'.")
|
||||
return
|
||||
var/node/statement/FunctionDefinition/func = globalScope.functions[name]
|
||||
var/datum/node/statement/FunctionDefinition/func = globalScope.functions[name]
|
||||
if(istype(func))
|
||||
var/node/statement/FunctionCall/stmt = new
|
||||
var/datum/node/statement/FunctionCall/stmt = new
|
||||
stmt.func_name = func.func_name
|
||||
stmt.parameters = params
|
||||
return RunFunction(stmt)
|
||||
@@ -137,4 +137,4 @@
|
||||
See Also:
|
||||
- <runtimeError>
|
||||
*/
|
||||
/n_Interpreter/proc/HandleError(runtimeError/e)
|
||||
/datum/n_Interpreter/proc/HandleError(datum/runtimeError/e)
|
||||
|
||||
@@ -13,13 +13,13 @@
|
||||
#define RETURNING 1
|
||||
#define BREAKING 2
|
||||
#define CONTINUING 4
|
||||
/n_Interpreter
|
||||
var/scope/curScope
|
||||
var/scope/globalScope
|
||||
var/node/BlockDefinition/program
|
||||
var/node/statement/FunctionDefinition/curFunction
|
||||
var/stack/scopes = new()
|
||||
var/stack/functions = new()
|
||||
/datum/n_Interpreter
|
||||
var/datum/scope/curScope
|
||||
var/datum/scope/globalScope
|
||||
var/datum/node/BlockDefinition/program
|
||||
var/datum/node/statement/FunctionDefinition/curFunction
|
||||
var/datum/stack/scopes = new()
|
||||
var/datum/stack/functions = new()
|
||||
|
||||
var/datum/container // associated container for interpeter
|
||||
/*
|
||||
@@ -46,7 +46,7 @@
|
||||
Constructor: New
|
||||
Calls <Load()> with the given parameters.
|
||||
*/
|
||||
/n_Interpreter/New(node/BlockDefinition/GlobalBlock/program=null)
|
||||
/datum/n_Interpreter/New(datum/node/BlockDefinition/GlobalBlock/program=null)
|
||||
.=..()
|
||||
if(program)Load(program)
|
||||
|
||||
@@ -54,20 +54,20 @@
|
||||
Proc: RaiseError
|
||||
Raises a runtime error.
|
||||
*/
|
||||
/n_Interpreter/proc/RaiseError(runtimeError/e)
|
||||
/datum/n_Interpreter/proc/RaiseError(datum/runtimeError/e)
|
||||
e.stack=functions.Copy()
|
||||
e.stack.Push(curFunction)
|
||||
src.HandleError(e)
|
||||
|
||||
/n_Interpreter/proc/CreateScope(node/BlockDefinition/B)
|
||||
var/scope/S = new(B, curScope)
|
||||
/datum/n_Interpreter/proc/CreateScope(datum/node/BlockDefinition/B)
|
||||
var/datum/scope/S = new(B, curScope)
|
||||
scopes.Push(curScope)
|
||||
curScope = S
|
||||
return S
|
||||
|
||||
/n_Interpreter/proc/CreateGlobalScope()
|
||||
/datum/n_Interpreter/proc/CreateGlobalScope()
|
||||
scopes.Clear()
|
||||
var/scope/S = new(program, null)
|
||||
var/datum/scope/S = new(program, null)
|
||||
globalScope = S
|
||||
return S
|
||||
|
||||
@@ -75,8 +75,8 @@
|
||||
Proc: RunBlock
|
||||
Runs each statement in a block of code.
|
||||
*/
|
||||
/n_Interpreter/proc/RunBlock(node/BlockDefinition/Block, scope/scope = null)
|
||||
var/is_global = istype(Block, /node/BlockDefinition/GlobalBlock)
|
||||
/datum/n_Interpreter/proc/RunBlock(datum/node/BlockDefinition/Block, datum/scope/scope = null)
|
||||
var/is_global = istype(Block, /datum/node/BlockDefinition/GlobalBlock)
|
||||
if(!is_global)
|
||||
if(scope)
|
||||
curScope = scope
|
||||
@@ -89,12 +89,12 @@ Runs each statement in a block of code.
|
||||
|
||||
if(cur_statements < max_statements)
|
||||
|
||||
for(var/node/statement/S in Block.statements)
|
||||
for(var/datum/node/statement/S in Block.statements)
|
||||
while(paused) sleep(10)
|
||||
|
||||
cur_statements++
|
||||
if(cur_statements >= max_statements)
|
||||
RaiseError(new/runtimeError/MaxCPU())
|
||||
RaiseError(new/datum/runtimeError/MaxCPU())
|
||||
|
||||
if(container && !alertadmins)
|
||||
if(istype(container, /datum/TCS_Compiler))
|
||||
@@ -106,8 +106,8 @@ Runs each statement in a block of code.
|
||||
message_admins(message, 1)
|
||||
break
|
||||
|
||||
if(istype(S, /node/statement/VariableAssignment))
|
||||
var/node/statement/VariableAssignment/stmt = S
|
||||
if(istype(S, /datum/node/statement/VariableAssignment))
|
||||
var/datum/node/statement/VariableAssignment/stmt = S
|
||||
var/name = stmt.var_name.id_name
|
||||
if(!stmt.object)
|
||||
// Below we assign the variable first to null if it doesn't already exist.
|
||||
@@ -120,38 +120,38 @@ Runs each statement in a block of code.
|
||||
var/datum/D = Eval(GetVariable(stmt.object.id_name))
|
||||
if(!D) return
|
||||
D.vars[stmt.var_name.id_name] = Eval(stmt.value)
|
||||
else if(istype(S, /node/statement/VariableDeclaration))
|
||||
else if(istype(S, /datum/node/statement/VariableDeclaration))
|
||||
//VariableDeclaration nodes are used to forcibly declare a local variable so that one in a higher scope isn't used by default.
|
||||
var/node/statement/VariableDeclaration/dec=S
|
||||
var/datum/node/statement/VariableDeclaration/dec=S
|
||||
if(!dec.object)
|
||||
AssignVariable(dec.var_name.id_name, null, curScope)
|
||||
else
|
||||
var/datum/D = Eval(GetVariable(dec.object.id_name))
|
||||
if(!D) return
|
||||
D.vars[dec.var_name.id_name] = null
|
||||
else if(istype(S, /node/statement/FunctionCall))
|
||||
else if(istype(S, /datum/node/statement/FunctionCall))
|
||||
RunFunction(S)
|
||||
else if(istype(S, /node/statement/FunctionDefinition))
|
||||
else if(istype(S, /datum/node/statement/FunctionDefinition))
|
||||
pass() //do nothing
|
||||
else if(istype(S, /node/statement/WhileLoop))
|
||||
else if(istype(S, /datum/node/statement/WhileLoop))
|
||||
RunWhile(S)
|
||||
else if(istype(S, /node/statement/IfStatement))
|
||||
else if(istype(S, /datum/node/statement/IfStatement))
|
||||
RunIf(S)
|
||||
else if(istype(S, /node/statement/ReturnStatement))
|
||||
else if(istype(S, /datum/node/statement/ReturnStatement))
|
||||
if(!curFunction)
|
||||
RaiseError(new/runtimeError/UnexpectedReturn())
|
||||
RaiseError(new/datum/runtimeError/UnexpectedReturn())
|
||||
continue
|
||||
status |= RETURNING
|
||||
returnVal=Eval(S:value)
|
||||
break
|
||||
else if(istype(S, /node/statement/BreakStatement))
|
||||
else if(istype(S, /datum/node/statement/BreakStatement))
|
||||
status |= BREAKING
|
||||
break
|
||||
else if(istype(S, /node/statement/ContinueStatement))
|
||||
else if(istype(S, /datum/node/statement/ContinueStatement))
|
||||
status |= CONTINUING
|
||||
break
|
||||
else
|
||||
RaiseError(new/runtimeError/UnknownInstruction())
|
||||
RaiseError(new/datum/runtimeError/UnknownInstruction())
|
||||
if(status)
|
||||
break
|
||||
|
||||
@@ -161,15 +161,15 @@ Runs each statement in a block of code.
|
||||
Proc: RunFunction
|
||||
Runs a function block or a proc with the arguments specified in the script.
|
||||
*/
|
||||
/n_Interpreter/proc/RunFunction(node/statement/FunctionCall/stmt)
|
||||
/datum/n_Interpreter/proc/RunFunction(datum/node/statement/FunctionCall/stmt)
|
||||
//Note that anywhere /node/statement/FunctionCall/stmt is used so may /node/expression/FunctionCall
|
||||
|
||||
// If recursion gets too high (max 50 nested functions) throw an error
|
||||
if(cur_recursion >= max_recursion)
|
||||
RaiseError(new/runtimeError/RecursionLimitReached())
|
||||
RaiseError(new/datum/runtimeError/RecursionLimitReached())
|
||||
return 0
|
||||
|
||||
var/node/statement/FunctionDefinition/def
|
||||
var/datum/node/statement/FunctionDefinition/def
|
||||
if(!stmt.object) //A scope's function is being called, stmt.object is null
|
||||
def = GetFunction(stmt.func_name)
|
||||
else if(istype(stmt.object)) //A method of an object exposed as a variable is being called, stmt.object is a /node/identifier
|
||||
@@ -182,14 +182,14 @@ Runs a function block or a proc with the arguments specified in the script.
|
||||
cur_recursion++ // add recursion
|
||||
if(istype(def))
|
||||
if(curFunction) functions.Push(curFunction)
|
||||
var/scope/S = CreateScope(def.block)
|
||||
var/datum/scope/S = CreateScope(def.block)
|
||||
for(var/i=1 to def.parameters.len)
|
||||
var/val
|
||||
if(stmt.parameters.len>=i)
|
||||
val = stmt.parameters[i]
|
||||
//else
|
||||
// unspecified param
|
||||
AssignVariable(def.parameters[i], new/node/expression/value/literal(Eval(val)), S)
|
||||
AssignVariable(def.parameters[i], new/datum/node/expression/value/literal(Eval(val)), S)
|
||||
curFunction=stmt
|
||||
RunBlock(def.block, S)
|
||||
//Handle return value
|
||||
@@ -201,11 +201,11 @@ Runs a function block or a proc with the arguments specified in the script.
|
||||
else
|
||||
cur_recursion--
|
||||
var/list/params=new
|
||||
for(var/node/expression/P in stmt.parameters)
|
||||
for(var/datum/node/expression/P in stmt.parameters)
|
||||
params+=list(Eval(P))
|
||||
if(isobject(def)) //def is an object which is the target of a function call
|
||||
if( !hascall(def, stmt.func_name) )
|
||||
RaiseError(new/runtimeError/UndefinedFunction("[stmt.object.id_name].[stmt.func_name]"))
|
||||
RaiseError(new/datum/runtimeError/UndefinedFunction("[stmt.object.id_name].[stmt.func_name]"))
|
||||
return
|
||||
return call(def, stmt.func_name)(arglist(params))
|
||||
else //def is a path to a global proc
|
||||
@@ -217,7 +217,7 @@ Runs a function block or a proc with the arguments specified in the script.
|
||||
Proc: RunIf
|
||||
Checks a condition and runs either the if block or else block.
|
||||
*/
|
||||
/n_Interpreter/proc/RunIf(node/statement/IfStatement/stmt)
|
||||
/datum/n_Interpreter/proc/RunIf(datum/node/statement/IfStatement/stmt)
|
||||
if(Eval(stmt.cond))
|
||||
RunBlock(stmt.block)
|
||||
else if(stmt.else_block)
|
||||
@@ -227,7 +227,7 @@ Checks a condition and runs either the if block or else block.
|
||||
Proc: RunWhile
|
||||
Runs a while loop.
|
||||
*/
|
||||
/n_Interpreter/proc/RunWhile(node/statement/WhileLoop/stmt)
|
||||
/datum/n_Interpreter/proc/RunWhile(datum/node/statement/WhileLoop/stmt)
|
||||
var/i=1
|
||||
while(Eval(stmt.cond) && Iterate(stmt.block, i++))
|
||||
continue
|
||||
@@ -237,10 +237,10 @@ Runs a while loop.
|
||||
Proc:Iterate
|
||||
Runs a single iteration of a loop. Returns a value indicating whether or not to continue looping.
|
||||
*/
|
||||
/n_Interpreter/proc/Iterate(node/BlockDefinition/block, count)
|
||||
/datum/n_Interpreter/proc/Iterate(datum/node/BlockDefinition/block, count)
|
||||
RunBlock(block)
|
||||
if(max_iterations > 0 && count >= max_iterations)
|
||||
RaiseError(new/runtimeError/IterationLimitReached())
|
||||
RaiseError(new/datum/runtimeError/IterationLimitReached())
|
||||
return 0
|
||||
if(status & (BREAKING|RETURNING))
|
||||
return 0
|
||||
@@ -251,36 +251,36 @@ Runs a single iteration of a loop. Returns a value indicating whether or not to
|
||||
Proc: GetFunction
|
||||
Finds a function in an accessible scope with the given name. Returns a <FunctionDefinition>.
|
||||
*/
|
||||
/n_Interpreter/proc/GetFunction(name)
|
||||
var/scope/S = curScope
|
||||
/datum/n_Interpreter/proc/GetFunction(name)
|
||||
var/datum/scope/S = curScope
|
||||
while(S)
|
||||
if(S.functions.Find(name))
|
||||
return S.functions[name]
|
||||
S = S.parent
|
||||
RaiseError(new/runtimeError/UndefinedFunction(name))
|
||||
RaiseError(new/datum/runtimeError/UndefinedFunction(name))
|
||||
|
||||
/*
|
||||
Proc: GetVariable
|
||||
Finds a variable in an accessible scope and returns its value.
|
||||
*/
|
||||
/n_Interpreter/proc/GetVariable(name)
|
||||
var/scope/S = curScope
|
||||
/datum/n_Interpreter/proc/GetVariable(name)
|
||||
var/datum/scope/S = curScope
|
||||
while(S)
|
||||
if(S.variables.Find(name))
|
||||
return S.variables[name]
|
||||
S = S.parent
|
||||
RaiseError(new/runtimeError/UndefinedVariable(name))
|
||||
RaiseError(new/datum/runtimeError/UndefinedVariable(name))
|
||||
|
||||
/n_Interpreter/proc/GetVariableScope(name) //needed for when you reassign a variable in a higher scope
|
||||
var/scope/S = curScope
|
||||
/datum/n_Interpreter/proc/GetVariableScope(name) //needed for when you reassign a variable in a higher scope
|
||||
var/datum/scope/S = curScope
|
||||
while(S)
|
||||
if(S.variables.Find(name))
|
||||
return S
|
||||
S = S.parent
|
||||
|
||||
|
||||
/n_Interpreter/proc/IsVariableAccessible(name)
|
||||
var/scope/S = curScope
|
||||
/datum/n_Interpreter/proc/IsVariableAccessible(name)
|
||||
var/datum/scope/S = curScope
|
||||
while(S)
|
||||
if(S.variables.Find(name))
|
||||
return TRUE
|
||||
@@ -297,13 +297,13 @@ name - The name of the variable to assign.
|
||||
value - The value to assign to it.
|
||||
S - The scope the variable resides in. If it is null, a scope with the variable already existing is found. If no scopes have a variable of the given name, the current scope is used.
|
||||
*/
|
||||
/n_Interpreter/proc/AssignVariable(name, node/expression/value, scope/S=null)
|
||||
/datum/n_Interpreter/proc/AssignVariable(name, datum/node/expression/value, datum/scope/S=null)
|
||||
if(!S) S = GetVariableScope(name)
|
||||
if(!S) S = curScope
|
||||
if(!S) S = globalScope
|
||||
ASSERT(istype(S))
|
||||
if(istext(value) || isnum(value) || isnull(value)) value = new/node/expression/value/literal(value)
|
||||
else if(!istype(value) && isobject(value)) value = new/node/expression/value/reference(value)
|
||||
if(istext(value) || isnum(value) || isnull(value)) value = new/datum/node/expression/value/literal(value)
|
||||
else if(!istype(value) && isobject(value)) value = new/datum/node/expression/value/reference(value)
|
||||
//TODO: check for invalid name
|
||||
S.variables["[name]"] = value
|
||||
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
Class: scope
|
||||
A runtime instance of a block. Used internally by the interpreter.
|
||||
*/
|
||||
/scope/
|
||||
var/scope/parent = null
|
||||
var/node/BlockDefinition/block
|
||||
/datum/scope
|
||||
var/datum/scope/parent = null
|
||||
var/datum/node/BlockDefinition/block
|
||||
var/list/functions
|
||||
var/list/variables
|
||||
|
||||
/scope/New(node/BlockDefinition/B, scope/parent)
|
||||
/datum/scope/New(datum/node/BlockDefinition/B, datum/scope/parent)
|
||||
src.block = B
|
||||
src.parent = parent
|
||||
src.variables = B.initial_variables.Copy()
|
||||
|
||||
Reference in New Issue
Block a user