easycoder 1__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of easycoder might be problematic. Click here for more details.

easycoder/__init__.py ADDED
@@ -0,0 +1,12 @@
1
+ '''EasyCoder for Python'''
2
+
3
+ from .ec_classes import *
4
+ from .ec_compiler import *
5
+ from .ec_condition import *
6
+ from .ec_core import *
7
+ from .ec_handler import *
8
+ from .ec_program import *
9
+ from .ec_timestamp import *
10
+ from .ec_value import *
11
+
12
+ __version__ = "1"
@@ -0,0 +1,51 @@
1
+ import sys
2
+
3
+ class FatalError:
4
+ def __init__(self, compiler, message):
5
+ compiler.showWarnings()
6
+ lino = compiler.tokens[compiler.index].lino
7
+ script = compiler.script.lines[lino].strip()
8
+ print(f'Compile error in {compiler.program.name} at line {lino + 1} ({script}): {message}')
9
+ sys.exit()
10
+
11
+ class AssertionError:
12
+ def __init__(self, program):
13
+ code = program.code[program.pc]
14
+ lino = code['lino']
15
+ script = program.script.lines[lino].strip()
16
+ print(f'Assertion Error in {program.name} at line {lino + 1}')
17
+ sys.exit()
18
+
19
+ class RuntimeError:
20
+ def __init__(self, program, message):
21
+ if program == None:
22
+ sys.exit(f'Runtime Error: {message}')
23
+ else:
24
+ code = program.code[program.pc]
25
+ lino = code['lino']
26
+ script = program.script.lines[lino].strip()
27
+ print(f'Runtime Error in {program.name} at line {lino + 1} ({script}): {message}')
28
+ sys.exit()
29
+
30
+ class RuntimeWarning:
31
+ def __init__(self, program, message):
32
+ if program == None:
33
+ print(f'Runtime Warning: {message}')
34
+ else:
35
+ code = program.code[program.pc]
36
+ lino = code['lino']
37
+ script = program.script.lines[lino].strip()
38
+ print(f'Runtime Warning in {program.name} at line {lino + 1} ({script}): {message}')
39
+
40
+ class Script:
41
+ def __init__(self, source):
42
+ self.lines = source.splitlines()
43
+ self.tokens = []
44
+
45
+ class Token:
46
+ def __init__(self, lino, token):
47
+ self.lino = lino
48
+ self.token = token
49
+
50
+ class Condition():
51
+ negate = False
@@ -0,0 +1,201 @@
1
+ from .ec_classes import Token, FatalError
2
+ from .ec_value import Value
3
+ from .ec_condition import Condition
4
+
5
+ class Compiler:
6
+
7
+ def __init__(self, program):
8
+ self.program = program
9
+ self.domains = self.program.domains
10
+ self.value = Value(self)
11
+ self.condition = Condition(self)
12
+ self.marker = 0
13
+ self.script = self.program.script
14
+ self.tokens = self.script.tokens
15
+ self.symbols = self.program.symbols
16
+ self.code = self.program.code
17
+ self.warnings = []
18
+ self.program.compiler = self
19
+ self.addCommand = self.program.add
20
+
21
+ def getPC(self):
22
+ return len(self.program.code)
23
+
24
+ def getIndex(self):
25
+ return self.index
26
+
27
+ # Move the index along
28
+ def next(self):
29
+ self.index += 1
30
+
31
+ # Get the current token
32
+ def getToken(self):
33
+ if self.index >= len(self.tokens):
34
+ FatalError(self, 'Premature end of script')
35
+ return self.tokens[self.index].token
36
+
37
+ # Get the next token
38
+ def nextToken(self):
39
+ self.index += 1
40
+ return self.getToken()
41
+
42
+ def peek(self):
43
+ try:
44
+ return self.tokens[self.index + 1].token
45
+ except:
46
+ return None
47
+
48
+ # Get a value
49
+ def getValue(self):
50
+ return self.value.compileValue()
51
+
52
+ # Get the next value
53
+ def nextValue(self):
54
+ self.index += 1
55
+ return self.value.compileValue()
56
+
57
+ # Get a constant
58
+ def getConstant(self, token):
59
+ self.index += 1
60
+ return self.value.compileConstant(token)
61
+
62
+ # Get a condition
63
+ def getCondition(self):
64
+ return self.condition.compileCondition()
65
+
66
+ # Get the next condition
67
+ def nextCondition(self):
68
+ self.index += 1
69
+ return self.condition.compileCondition()
70
+
71
+ def tokenIs(self, value):
72
+ return self.getToken() == value
73
+
74
+ def nextIs(self, value):
75
+ return self.nextToken() == value
76
+
77
+ def getCommandAt(self, pc):
78
+ return self.program.code[pc]
79
+
80
+ def isSymbol(self):
81
+ token=self.getToken()
82
+ try:
83
+ self.symbols[token]
84
+ except:
85
+ return False
86
+ return True
87
+
88
+ def nextIsSymbol(self):
89
+ self.next()
90
+ return self.isSymbol()
91
+
92
+ def rewindTo(self, index):
93
+ self.index = index
94
+
95
+ def getLino(self):
96
+ if self.index >= len(self.tokens):
97
+ return 0
98
+ return self.tokens[self.index].lino
99
+
100
+ def warning(self, message):
101
+ self.warnings.append(message)
102
+
103
+ def showWarnings(self):
104
+ for warning in self.warnings:
105
+ print(f'Line {self.getLino() + 1}: {warning}')
106
+
107
+ def getSymbolRecord(self):
108
+ token = self.getToken()
109
+ symbol = self.symbols[token]
110
+ if symbol != None:
111
+ symbolRecord = self.code[symbol]
112
+ symbolRecord['used'] = True
113
+ return symbolRecord
114
+ return None
115
+
116
+ def compileLabel(self, command):
117
+ return self.compileSymbol(command, self.getToken(), False)
118
+
119
+ def compileVariable(self, command, valueHolder = False):
120
+ return self.compileSymbol(command, self.nextToken(), valueHolder)
121
+
122
+ def compileSymbol(self, command, name, valueHolder):
123
+ try:
124
+ v = self.symbols[name]
125
+ except:
126
+ v = None
127
+ if v:
128
+ FatalError(self, f'Duplicate symbol name "{name}"')
129
+ return False
130
+ self.symbols[name] = self.getPC()
131
+ command['type'] = 'symbol'
132
+ command['valueHolder'] = valueHolder
133
+ command['name'] = name
134
+ command['elements'] = 1
135
+ command['index'] = 0
136
+ command['value'] = [None]
137
+ command['used'] = False
138
+ command['debug'] = False
139
+ self.addCommand(command)
140
+ return True
141
+
142
+ # Compile the current token
143
+ def compileToken(self):
144
+ token = self.getToken()
145
+ # print(f'Compile {token}')
146
+ if not token:
147
+ return False
148
+ mark = self.getIndex()
149
+ for domain in self.domains:
150
+ handler = domain.keywordHandler(token)
151
+ if handler:
152
+ command = {}
153
+ command['domain'] = domain.getName()
154
+ command['lino'] = self.tokens[self.index].lino
155
+ command['keyword'] = token
156
+ command['type'] = None
157
+ command['debug'] = True
158
+ result = handler(command)
159
+ if result:
160
+ return result
161
+ else:
162
+ self.rewindTo(mark)
163
+ else:
164
+ self.rewindTo(mark)
165
+ FatalError(self, f'No handler found for "{token}"')
166
+ return False
167
+
168
+ # Compile a single command
169
+ def compileOne(self):
170
+ keyword = self.getToken()
171
+ if not keyword:
172
+ return False
173
+ # print(f'Compile keyword "{keyword}"')
174
+ if keyword.endswith(':'):
175
+ command = {}
176
+ command['domain'] = None
177
+ command['lino'] = self.tokens[self.index].lino
178
+ return self.compileLabel(command)
179
+ else:
180
+ return self.compileToken()
181
+
182
+ # Compile the script
183
+ def compileFrom(self, index, stopOn):
184
+ self.index = index
185
+ while True:
186
+ token = self.tokens[self.index]
187
+ keyword = token.token
188
+ # line = self.script.lines[token.lino]
189
+ # print(f'{keyword} - {line}')
190
+ # if keyword != 'else':
191
+ if self.compileOne() == True:
192
+ if self.index == len(self.tokens) - 1:
193
+ return True
194
+ token = self.nextToken()
195
+ if token in stopOn:
196
+ return True
197
+ else:
198
+ return False
199
+
200
+ def compileFromHere(self, stopOn):
201
+ return self.compileFrom(self.getIndex(), stopOn)
@@ -0,0 +1,26 @@
1
+ class Condition:
2
+
3
+ def __init__(self, compiler):
4
+ self.domains = compiler.domains
5
+ self.getToken = compiler.getToken
6
+ self.nextToken = compiler.nextToken
7
+ self.peek = compiler.peek
8
+ self.getIndex = compiler.getIndex
9
+ self.tokenIs = compiler.tokenIs
10
+ self.rewindTo = compiler.rewindTo
11
+ self.program = compiler.program
12
+
13
+ def compileCondition(self):
14
+ mark = self.getIndex()
15
+ for domain in self.domains:
16
+ condition = domain.compileCondition()
17
+ if condition != None:
18
+ condition.domain= domain.getName()
19
+ return condition
20
+ self.rewindTo(mark)
21
+ return None
22
+
23
+ def testCondition(self, condition):
24
+ handler = self.program.domainIndex[condition.domain]
25
+ handler = handler.conditionHandler(condition.type)
26
+ return handler(condition)