Created
November 27, 2024 04:07
-
-
Save xcombelle/1ee28a3de056b431723cef78849e7509 to your computer and use it in GitHub Desktop.
prefix "forth"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Simply: | |
def __init__(self, input): | |
self.input = input | |
def next(self): | |
end = self.input.index(" ") | |
token = self.input[:end] | |
self.input = self.input[end+1:] | |
return token | |
def one(self): | |
token = self.next() | |
return self.eval(token) | |
def eval(self, token): | |
f = d[token] | |
return f(self) | |
d = { | |
"add": lambda s: s.one() + s.one(), | |
"mult": lambda s: s.one() * s.one(), | |
"print": lambda s: print(s.one()), | |
"int": lambda s: int(s.next()), | |
} | |
s = Simply('''print mult int 2 add int 3 int 5 ''') | |
s.one() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment