diff --git a/code.txt b/code.txt index 2f6793e..2438b39 100755 --- a/code.txt +++ b/code.txt @@ -8,18 +8,7 @@ EndNote System Print BeginText Hello, there! EndText Done +System Print BeginText Please enter your name: EndText Done Set Name To System Read EndSet BeginNote Reads an entire line EndNote - -Set Friends To List New EndSet -Friends Add BeginText Jookia EndText Done -Friends Add BeginText BadGuy EndText Done -Friends Add BeginText GoodGuy EndText Done -Friends Add BeginText Friendo EndText Done - -Set Mistake To Friends Find BeginText BadGuy EndText EndSet -Friends Remove Mistake Done - -If Friends Contains Name -Then System Print BeginText Hi friend! EndText -Else System Print BeginText Hi stranger! EndText -EndIf +System Print BeginText Hi there, EndText Done +System Print Name Done diff --git a/main.py b/main.py index 074b877..288fe39 100755 --- a/main.py +++ b/main.py @@ -60,14 +60,14 @@ sys.exit(1) (type, value) = parser_tokens[parser_pos] parser_pos += 1 - #print("Read %s %s" % (type, value)) + print("Read %s %s" % (type, value)) return (type, value) def parser_peek(): global parser_pos global parser_tokens (type, value) = parser_tokens[parser_pos] - #print("Peeked %s %s" % (type, value)) + print("Peeked %s %s" % (type, value)) return (type, value) def parser_skip(): @@ -128,7 +128,7 @@ print("While parsing arguments") return None print("Parsed statement: subject %s verb %s args %s" % (subject, verb, arguments)) - return ('Statement', subject, verb, arguments) + return ('statement', subject, verb, arguments) def parse_set(): subject = parse_subject() @@ -145,7 +145,7 @@ print("While parsing statement") return None print("Parsed set for %s" % (subject)) - return ('Set', subject, ast) + return ('set', subject, ast) def parse_if(): print("Parsing if test condition...") @@ -164,7 +164,7 @@ print("While parsing failure statement") return None print("Parsed conditional") - return ('If', test, success, failure) + return ('if', test, success, failure) def parse_directive(): (type, value) = parser_peek() @@ -215,6 +215,52 @@ print("Parsed file") return ast +def do_system_print(env, args): + (text_type, text_value) = args[0] + if text_type == "symbol": + (text_type, text_value) = env[text_value] + if text_type != "text": + print("Invalid print value: %s" % (text_type)) + return None + else: + print(text_value) + return None + +def do_system_read(env, args): + return ('text', input()) + +base_env = { + "system": { + "type": "module", + "print": do_system_print, + "read": do_system_read, + } +} + +def run_statement(env, ast): + command = env[ast[1]][ast[2]] + return command(env, ast[3]) + +def run_set(env, ast): + env[ast[1]] = run_statement(env, ast[2]) + return env[ast[1]] + +def run_if(env, ast): + print("Unimplemented if") + return None + +def run_command(env, ast): + type = ast[0] + if type == "statement": + return run_statement(env, ast) + elif type == "set": + return run_set(env, ast) + elif type == "if": + return run_if(env, ast) + else: + print("Unknown command type %s" % (ast)) + return None + def main(args): if len(args) != 2: print("Usage: main.py FILENAME") @@ -229,6 +275,8 @@ ast = parse_file() if not ast: return 1 + for command in ast: + run_command(base_env, command) return 0 if __name__ == "__main__":