one-file-projects/gerlang.py

240 lines
5.6 KiB
Python
Raw Normal View History

2013-09-26 21:59:25 +02:00
class Analyzer:
def __init__(self, itr):
self.itr = itr
def next(self,amount=1):
if len(self.itr) == 0: return None
if amount == 1: return self.itr[0]
return self.itr[:amount]
def take(self,amount=1):
m, self.itr = self.next(amount), self.itr[amount:]
return m
def __len__(self):
return len(self.itr)
class StringAnalyzer(Analyzer):
def nextOrd(self):
m = self.next()
if m is None: return None
return ord(m)
def between(self,cmin,cmax):
c = self.nextOrd()
if c is None: return False
return c >= ord(cmin) and c <= ord(cmax)
def is_a(self,c):
return self.next() == c
class TokenListAnalyzer(Analyzer):
def takeUntilType(self,end):
2013-10-22 15:41:11 +02:00
return self.takeUntil(lambda t: t[0] == end)
def takeUntil(self,condition, dropEnd=True):
2013-09-26 21:59:25 +02:00
t = []
2013-10-22 15:41:11 +02:00
while self.next() is not None and not condition(self.next()):
2013-09-26 21:59:25 +02:00
t.append( self.take() )
2013-10-22 15:41:11 +02:00
if dropEnd and self.next() is not None:
self.take()
2013-09-26 21:59:25 +02:00
return t
2013-10-22 15:41:11 +02:00
2013-09-26 21:59:25 +02:00
class Lexer:
keywords = ["setze","auf","durch","schreibe"]
operators = ["plus","minus","mal","geteilt"]
IDENT = 0
KEYWORD = 1
INT = 2
FLOAT = 3
OP = 4
BRACE_OPEN = 5
BRACE_CLOSE = 6
NEWLINE = 7
def lex(self, source):
tokens = []
sa = StringAnalyzer(source)
braces = 0
2013-10-22 15:41:11 +02:00
stack = 0
2013-09-26 21:59:25 +02:00
while len(sa) != 0:
if sa.between('a', 'z') or sa.between('A', 'Z'): #identifier or keyword
ident = ""
while sa.between('a', 'z') or sa.between('A', 'Z') or sa.between('0', '9') or sa.is_a('_'):
ident += sa.take()
if ident.lower() in self.keywords:
tokens.append( (self.KEYWORD,ident.lower()) )
elif ident.lower() in self.operators:
tokens.append( (self.OP,ident.lower()) )
2013-10-22 15:41:11 +02:00
elif ident.lower() == "wenn":
tokens.append( ( self.KEYWORD, "wenn", stack ) )
stack += 1
elif ident.lower() == "ende":
stack -= 1
tokens.append( ( self.KEYWORD, "ende", stack ) )
2013-09-26 21:59:25 +02:00
else:
tokens.append( (self.IDENT,ident) )
elif sa.between('0', '9'): #number
num = ""
t = (self.INT, int)
while sa.between('0', '9'):
num += sa.take()
if sa.is_a(','):
t = (self.FLOAT, float)
sa.take()
num += "."
while sa.between('0', '9'):
num += sa.take()
tokens.append( (t[0],t[1](num)) )
elif sa.is_a('('):
tokens.append( (self.BRACE_OPEN,braces) )
braces+=1
elif sa.is_a(')'):
braces-=1
tokens.append( (self.BRACE_CLOSE,braces) )
elif sa.is_a('\n'):
tokens.append( (self.NEWLINE,) )
sa.take()
elif sa.is_a(' ') or sa.is_a('\t') or sa.is_a('\r'):
sa.take()
else:
raise ParserException("WTF is %s" % sa.take() )
return tokens
class ParserException(Exception):
pass
class Parser:
def parse(self,tokens):
block = BlockTerm()
ta = TokenListAnalyzer(tokens)
while len(ta) > 0:
if ta.next()[0] == Lexer.KEYWORD:
if ta.next()[1] == "setze":
ta.take()
if ta.next()[0] != Lexer.IDENT:
raise ParseException("missing identifier after setze")
ident = ta.take()[1]
if ta.next()[0] != Lexer.KEYWORD or ta.next()[1] != "auf":
raise ParserException("missing auf after identifier")
ta.take()
2013-10-22 15:41:11 +02:00
term = self.__parseTerm(ta.takeUntil(lambda t: t[0] == Lexer.NEWLINE))
2013-09-26 21:59:25 +02:00
block.append(AssignmentTerm(ident,term))
elif ta.next()[1] == "schreibe":
ta.take()
term = self.__parseTerm(ta.takeUntilType(Lexer.NEWLINE))
block.append(PrintTerm(term))
2013-10-22 15:41:11 +02:00
elif ta.next()[1] == "wenn":
stack = ta.next()[2]
ta.take()
condition = ta.takeUntil(lambda t: t[0] == Lexer.NEWLINE)
b = ta.takeUntil(lambda t: t[0] == Lexer.KEYWORD and t[1] == "ende" and t[2] == stack)
block.append( ConditionalTerm(self.__parseTerm(condition), self.parse(b) ) )
else:
raise Exception("what? %s" % str(ta.next()))
2013-09-26 21:59:25 +02:00
elif ta.next()[0] == Lexer.NEWLINE:
ta.take()
2013-10-22 15:41:11 +02:00
else:
raise Exception("huh? %s" % str(ta.next()))
2013-09-26 21:59:25 +02:00
return block
def __parseTerm(self,tokens):
t = tokens[0]
2013-10-22 15:41:11 +02:00
if t[0] == Lexer.INT or t[0] == Lexer.FLOAT:
2013-09-26 21:59:25 +02:00
return ValueTerm(t[1])
2013-10-22 15:41:11 +02:00
elif t[0] == Lexer.IDENT:
return IdentifierTerm(t[1])
2013-09-26 21:59:25 +02:00
else:
2013-10-22 15:41:11 +02:00
return ValueTerm(0)
2013-09-26 21:59:25 +02:00
class Context():
pass
class SubContext(Context):
pass
2013-10-22 15:41:11 +02:00
class Term(object):
2013-09-26 21:59:25 +02:00
def run(self,context):
raise Exception("get_value must be overwritten")
2013-10-22 15:41:11 +02:00
class Operator2(Term):
token = None
priority = 0
def __init__(self, left, right):
self.left = left
self.right = right
def run(self,context):
return self.calc( self.left.run(context), self.right.run(context) )
def calc(self, r, l):
raise Exception("calc not implemented")
2013-09-26 21:59:25 +02:00
class IdentifierTerm:
def __init__(self, ident):
self.ident = ident
def run(self,context):
return context[self.ident]
class ValueTerm:
def __init__(self, value):
self.value = value
def run(self,context):
return self.value
class AssignmentTerm(Term):
def __init__(self,ident,term):
self.ident = ident
self.term = term
def run(self,context):
context[self.ident] = self.term.run(context)
return None
class PrintTerm(Term):
def __init__(self,term):
self.term = term
def run(self,context):
print self.term.run(context)
return None
2013-10-22 15:41:11 +02:00
class ConditionalTerm(Term):
def __init__(self,condition,block):
self.condition = condition
self.block = block
def run(self,context):
m = self.condition.run(context)
if m != 0:
self.block.run(context)
2013-09-26 21:59:25 +02:00
class BlockTerm(Term):
def __init__(self):
self.terms = []
def append(self,item):
self.terms.append(item)
def run(self,context):
result = None
for term in self.terms:
result = term.run(context)
return result
def main():
context = {}
while True:
code = raw_input(">>> ")
2013-10-22 15:41:11 +02:00
tokens = Lexer().lex(code)
term = Parser().parse(tokens)
term.run(context)
2013-09-26 21:59:25 +02:00
if __name__ == '__main__':
main()