Skip to content

Commit

Permalink
Merge pull request #108 from Jodus-Melodus/Jodus-Melodus/issue94
Browse files Browse the repository at this point in the history
Evaluate function
  • Loading branch information
Jodus-Melodus authored Jun 17, 2024
2 parents 3fd00a2 + aef1f30 commit 4a72f29
Show file tree
Hide file tree
Showing 16 changed files with 448 additions and 445 deletions.
9 changes: 5 additions & 4 deletions Themes/night-owl/syntax.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
".phi": {
"keywords": [
"#3d81ff",
"(?<!\\w)(output|for|readFile|del|update|hasAttr|unknown|in|case|match|input|throw|each|try|catch|continue|break|type|while|if|int|Real|Int|Str|as|lambda|real|import|export|obj|array|bool|str|items|now|length|wait|hash|fn|do|else|append|keys|join|format)(?!\\w)",
"(?<!\\w)(output|eval|for|readFile|del|update|hasAttr|unknown|in|case|match|input|throw|each|try|catch|continue|break|type|while|if|int|Real|Int|Str|as|lambda|real|import|export|obj|array|bool|str|items|now|length|wait|hash|fn|do|else|append|keys|join|format)(?!\\w)",
[
"output",
"input",
Expand Down Expand Up @@ -48,7 +48,8 @@
".items",
".update",
".keys",
".hasAttr"
".hasAttr",
"eval"
]
],
"symbols": [
Expand Down Expand Up @@ -85,7 +86,7 @@
]
]
},
".json":{
".json": {
"symbols": [
"#ffb617",
"(:|\")",
Expand All @@ -97,5 +98,5 @@
[]
]
},
".txt":{}
".txt": {}
}
5 changes: 3 additions & 2 deletions Themes/phi-theme/syntax.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
".phi": {
"keywords": [
"#3d81ff",
"(?<!\\w)(output|unknown|del|hasAttr|for|in|readFile|case|update|match|input|throw|each|try|catch|continue|break|type|while|if|int|Real|Int|Str|as|lambda|real|import|export|obj|array|bool|str|items|now|length|wait|hash|fn|do|else|append|keys|join|format)(?!\\w)",
"(?<!\\w)(output|eval|unknown|del|hasAttr|for|in|readFile|case|update|match|input|throw|each|try|catch|continue|break|type|while|if|int|Real|Int|Str|as|lambda|real|import|export|obj|array|bool|str|items|now|length|wait|hash|fn|do|else|append|keys|join|format)(?!\\w)",
[
"output",
"input",
Expand Down Expand Up @@ -48,7 +48,8 @@
".items",
".keys",
".update",
".hasAttr"
".hasAttr",
"eval"
]
],
"symbols": [
Expand Down
Binary file modified __pycache__/shell.cpython-311.pyc
Binary file not shown.
39 changes: 19 additions & 20 deletions backend/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import sys

class Environment:
def __init__(self, parent=None, file_path:str='') -> None:
def __init__(self, parent=None, file_path:str="") -> None:
self.file_path = file_path
self.parent = parent
self.variables = {}
self.constants = {}

def __str__(self) -> str:
return 'Environment'
return "Environment"

def __repr__(self) -> str:
return str({
Expand All @@ -31,7 +31,7 @@ def assign_variable(self, variable_name: str, var_value) -> None:
return var_value

def declare_variable(self, variable_name: str, var_value, constant:bool=False) -> None:
if ((variable_name in self.variables) or (variable_name in self.constants)) and (variable_name != '~'):
if ((variable_name in self.variables) or (variable_name in self.constants)) and (variable_name != "~"):
return SyntaxError(self.file_path, self, f"Variable '{variable_name}' already defined.", 0, 0)
if constant:
self.constants[variable_name] = var_value
Expand Down Expand Up @@ -64,26 +64,25 @@ def delete_variable(self, variable_name:str) -> None:
env = self.resolve(variable_name)
del env.variables[variable_name]

def create_global_environment(parent:Environment|None=None, file_path:str='') -> Environment:
def create_global_environment(parent:Environment|None=None, file_path:str="") -> Environment:
env = Environment(parent, file_path)
# functions
env.declare_variable('output', NativeFunction(lambda args, scope : sys.stdout.write(str(output(args[0], file_path)) + '\n')), True)
env.declare_variable('input', NativeFunction(lambda args, scope : phi_input(file_path, args[0])), True)
env.declare_variable('type', NativeFunction(lambda args, scope : type_(file_path, args[0])), True)
env.declare_variable('hash', NativeFunction(lambda args, scope : hash(file_path, args[0])), True)
env.declare_variable('Str', NativeFunction(lambda args, scope: hard_cast_string(file_path, args[0])), True)
env.declare_variable('Int', NativeFunction(lambda args, scope: hard_cast_integer(file_path, args[0])), True)
env.declare_variable('Real', NativeFunction(lambda args, scope: hard_cast_real(file_path, args[0])), True)
env.declare_variable('readFile', NativeFunction(lambda args, scope: read_file(file_path, args[0])), True)

# Move to modules
env.declare_variable('now', NativeFunction(lambda args, scope : now(file_path, )), True)
env.declare_variable('wait', NativeFunction(lambda args, scope : wait(file_path, args[0])), True)
env.declare_variable("output", NativeFunction(lambda args, scope : sys.stdout.write(str(output(args[0], file_path)) + "\n")), True)
env.declare_variable("input", NativeFunction(lambda args, scope : phi_input(file_path, args[0])), True)
env.declare_variable("type", NativeFunction(lambda args, scope : type_(file_path, args[0])), True)
env.declare_variable("hash", NativeFunction(lambda args, scope : hash(file_path, args[0])), True)
env.declare_variable("Str", NativeFunction(lambda args, scope: hard_cast_string(file_path, args[0])), True)
env.declare_variable("Int", NativeFunction(lambda args, scope: hard_cast_integer(file_path, args[0])), True)
env.declare_variable("Real", NativeFunction(lambda args, scope: hard_cast_real(file_path, args[0])), True)
env.declare_variable("readFile", NativeFunction(lambda args, scope: read_file(file_path, args[0])), True)
env.declare_variable("now", NativeFunction(lambda args, scope : now(file_path, )), True)
env.declare_variable("wait", NativeFunction(lambda args, scope : wait(file_path, args[0])), True)
env.declare_variable("eval", NativeFunction(lambda args, scope : phi_evaluate(file_path, args[0])), True)

# variables
env.declare_variable('_', NullValue(), True)
env.declare_variable('?', UnknownValue(NullValue()))
env.declare_variable('T', BooleanValue("T"), True)
env.declare_variable('F', BooleanValue("F"), True)
env.declare_variable("_", NullValue(), True)
env.declare_variable("?", UnknownValue(NullValue()))
env.declare_variable("T", BooleanValue("T"), True)
env.declare_variable("F", BooleanValue("F"), True)

return env
4 changes: 4 additions & 0 deletions backend/Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def phi_input(file_path, arg:StringValue) -> StringValue:
sys.stdout.write(arg.value)
return StringValue(sys.stdin.readline().strip(), arg.line, arg.column)

def phi_evaluate(file_path: str, arg:StringValue) -> NullValue:
from shell import run
return run(arg.value, file_path)

def now(file_path) -> IntegerValue:
return IntegerValue(time())

Expand Down
Loading

0 comments on commit 4a72f29

Please sign in to comment.