Files
  • main.py
  • example.z
  • fib.z
  • lexer.py
  • parser.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from parser import Zarser, FinishFunc


class State:

  def __init__(self, in_func: bool=False):
    self.globs = {}
    self.local = {}
    self.funcs = {}
    self.in_func = in_func
    self.bad_if = None
    self.defining = ""

  def __getitem__(self, name):
    if name in self.local:
      return self.local[name]
    elif name in self.globs:
      return self.globs[name]
    else:
      raise ValueError("Unable to find variable " + name)

  def __setitem__(self, name, value):
    if self.in_func:
      self.local[name] = value
    else:
      self.globs[name] = value

  def call(self, func_name, args):
    if not isinstance(args, list):
      args = [args]
    new_state = self.__class__(True)
    new_state.globs = self.globs
    new_state.funcs = self.funcs
    new_state.local = dict(zip(self.funcs[func_name][1], args))
    try:
      for line in self.funcs[func_name][0]:
        Zarser(new_state).parse(line)
    except FinishFunc as e:
      return e.args[0]
    finally:
      pass
      # print(line)


print("RUNNING EXAMPLE\n")
with open('example.z') as file:
  try:
    state = State()
    for line in file.read().split('\n'):
      l = line.strip()
      if l:
        Zarser(state).parse(l)
  finally:
    print('')
    # print(state.funcs, line)

print("RUNNING FIB\n")
with open('fib.z') as file:
  try:
    state = State()
    for line in file.read().split('\n'):
      l = line.strip()
      if l:
        Zarser(state).parse(l)
  finally:
    print('')
    # print(state.funcs, line)