From 680626f1258611cb900e4e10e31ffcbe1b2dbedc Mon Sep 17 00:00:00 2001 From: rmgr Date: Wed, 29 Jun 2022 17:09:31 +0930 Subject: [PATCH] Add basic string support --- rforth.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/rforth.py b/rforth.py index 0d8bfed..a470bb7 100755 --- a/rforth.py +++ b/rforth.py @@ -1,9 +1,10 @@ #!/usr/bin/python3 import argparse stack = [] -heap = [0] * 20 +heap = [0] * 100 next_heap = 0 compiling = False +reading_string = False words = {} word_name = "" @@ -19,7 +20,7 @@ def get_input(): def parse_input(input_string, say_ok=True): while len(input_string) > 0: string = str(input_string[0]) - if not compiling: + if not compiling and not reading_string: if string.isnumeric(): add_to_stack(string) elif string == "stack": @@ -66,7 +67,14 @@ def parse_input(input_string, say_ok=True): dump_heap() elif string == "alloc": alloc() - else: + elif string == 's"': + start_reading_string() + elif reading_string: + if string == '"': + stop_reading_string() + else: + add_new_word_to_string(string) + elif compiling and not reading_string: if string == ";": stop_compiling() else: @@ -74,7 +82,9 @@ def parse_input(input_string, say_ok=True): input_string = get_next(input_string) if say_ok: print("ok.") - +def add_new_word_to_string(string): + for char in string[::-1]: + stack.insert(0, ord(char)) def dump_heap(): print(heap) @@ -179,6 +189,14 @@ def add_to_new_word(value): else: words[word_name] = words[word_name] + value + " " +def start_reading_string(): + global reading_string + reading_string = True + +def stop_reading_string(): + global reading_string + reading_string = False + def start_compiling(): global compiling compiling = True