Skip to content

Instantly share code, notes, and snippets.

@filipevarjao
Created February 21, 2023 12:59
Show Gist options
  • Save filipevarjao/255a2c7248363f9447d68e89a340713e to your computer and use it in GitHub Desktop.
Save filipevarjao/255a2c7248363f9447d68e89a340713e to your computer and use it in GitHub Desktop.
chatGPT compiler
import subprocess
# AST Node Types
class Statement:
pass
class Expression:
pass
class Assign(Statement):
def __init__(self, name, value):
self.name = name
self.value = value
class Print(Statement):
def __init__(self, expr):
self.expr = expr
class Number(Expression):
def __init__(self, value):
self.value = value
class BinaryOp(Expression):
def __init__(self, op, left, right):
self.op = op
self.left = left
self.right = right
class Variable(Expression):
def __init__(self, name):
self.name = name
# The Parser
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.pos = 0
self.current_token = None
def consume(self):
self.pos += 1
if self.pos > len(self.tokens):
self.current_token = None
else:
self.current_token = self.tokens[self.pos - 1]
def parse(self):
program = []
self.current_token = self.tokens[0]
while self.current_token is not None:
program.append(self.parse_statement())
return program
def parse_statement(self):
if self.current_token.type == 'NAME':
name = self.current_token.value
self.consume()
if self.current_token.type == 'EQUAL':
self.consume()
value = self.parse_expression()
return Assign(name, value)
else:
raise Exception('Syntax error: expected "="')
elif self.current_token.type == 'PRINT':
self.consume()
expr = self.parse_expression()
return Print(expr)
else:
raise Exception('Syntax error: expected statement')
def parse_expression(self):
left = self.parse_term()
while self.current_token is not None and self.current_token.type in ('PLUS', 'MINUS'):
op = self.current_token
self.consume()
right = self.parse_term()
left = BinaryOp(op.type, left, right)
return left
def parse_term(self):
if self.current_token.type == 'NUMBER':
value = self.current_token.value
self.consume()
return Number(value)
elif self.current_token.type == 'NAME':
name = self.current_token.value
self.consume()
return Variable(name)
else:
raise Exception('Syntax error: expected number or variable')
# The Interpreter
class Interpreter:
def __init__(self):
self.vars = {}
def interpret(self, program):
for statement in program:
if isinstance(statement, Assign):
value = self.eval_expression(statement.value)
self.vars[statement.name] = value
elif isinstance(statement, Print):
value = self.eval_expression(statement.expr)
print(value)
else:
raise Exception('Unknown statement type')
def eval_expression(self, expr):
if isinstance(expr, Number):
return int(expr.value)
elif isinstance(expr, Variable):
name = expr.name
if name in self.vars:
return self.vars[name]
else:
raise Exception('Undefined variable: ' + name)
elif isinstance(expr, BinaryOp):
left = self.eval_expression(expr.left)
right = self.eval_expression(expr.right)
if expr.op == 'PLUS':
return left + right
elif expr.op == 'MINUS':
return left - right
else:
raise Exception('Unknown operator: ' + expr.op)
else:
raise Exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment