circuit-lang 0.5.0__tar.gz
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.
- circuit_lang-0.5.0/PKG-INFO +18 -0
- circuit_lang-0.5.0/README.md +3 -0
- circuit_lang-0.5.0/circuit/__init__.py +2 -0
- circuit_lang-0.5.0/circuit/__main__.py +3 -0
- circuit_lang-0.5.0/circuit/compiler.py +139 -0
- circuit_lang-0.5.0/circuit/vm.py +194 -0
- circuit_lang-0.5.0/circuit_lang.egg-info/PKG-INFO +18 -0
- circuit_lang-0.5.0/circuit_lang.egg-info/SOURCES.txt +11 -0
- circuit_lang-0.5.0/circuit_lang.egg-info/dependency_links.txt +1 -0
- circuit_lang-0.5.0/circuit_lang.egg-info/entry_points.txt +2 -0
- circuit_lang-0.5.0/circuit_lang.egg-info/top_level.txt +1 -0
- circuit_lang-0.5.0/pyproject.toml +34 -0
- circuit_lang-0.5.0/setup.cfg +4 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: circuit-lang
|
|
3
|
+
Version: 0.5.0
|
|
4
|
+
Summary: The Circuit programming language and CVM.
|
|
5
|
+
Author: Olan
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: circuit,compiler,vm,cvm,stack-based
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
13
|
+
Requires-Python: >=3.8
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# Circuit!
|
|
17
|
+
Circuit 0.5-0 is out!
|
|
18
|
+
the public beta!
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
from .vm import Vm
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
vm = Vm()
|
|
5
|
+
version = "0.5-0"
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def compile_line(line):
|
|
9
|
+
line = line.strip().lstrip(" \t")
|
|
10
|
+
if line.startswith("push "):
|
|
11
|
+
content = line[5:]
|
|
12
|
+
return f"PUSH {content}"
|
|
13
|
+
elif line.startswith("store "):
|
|
14
|
+
name = line[6:]
|
|
15
|
+
return f"STORE {name}"
|
|
16
|
+
elif line.startswith("load "):
|
|
17
|
+
name = line[5:]
|
|
18
|
+
return f"LOAD {name}"
|
|
19
|
+
elif line in ("print", "printnl"):
|
|
20
|
+
return line.upper()
|
|
21
|
+
elif line == "input":
|
|
22
|
+
return "INPUT"
|
|
23
|
+
elif line in ("add", "sub", "mul", "div", "eqs", "neq", "lt", "gt", "lte", "gte"):
|
|
24
|
+
return line.upper()
|
|
25
|
+
elif line.startswith("//"):
|
|
26
|
+
return ""
|
|
27
|
+
elif line.startswith("/*") and line.endswith("*/"):
|
|
28
|
+
return ""
|
|
29
|
+
elif line in ("arr", "clear"):
|
|
30
|
+
return line.upper()
|
|
31
|
+
elif line == "concat":
|
|
32
|
+
return "CONCAT"
|
|
33
|
+
elif line in ("if", "end"):
|
|
34
|
+
return line.upper()
|
|
35
|
+
elif line == "":
|
|
36
|
+
return ""
|
|
37
|
+
elif line.startswith("rand "):
|
|
38
|
+
content = line[5:]
|
|
39
|
+
return f"RAND {content}"
|
|
40
|
+
elif line == "del":
|
|
41
|
+
return line.upper()
|
|
42
|
+
else:
|
|
43
|
+
raise SyntaxError("invalid code")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def run_compiled(compiled):
|
|
47
|
+
vm.run("\n".join(compiled))
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def repl():
|
|
51
|
+
print("Circuit REPL (CTRL+D to exit)")
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
while True:
|
|
55
|
+
line = input("> ")
|
|
56
|
+
|
|
57
|
+
if not line.strip():
|
|
58
|
+
continue
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
compiled_line = compile_line(line)
|
|
62
|
+
run_compiled([compiled_line])
|
|
63
|
+
except Exception as e:
|
|
64
|
+
print(e)
|
|
65
|
+
|
|
66
|
+
except EOFError:
|
|
67
|
+
print()
|
|
68
|
+
sys.exit()
|
|
69
|
+
|
|
70
|
+
def main():
|
|
71
|
+
if len(sys.argv) == 1:
|
|
72
|
+
repl()
|
|
73
|
+
|
|
74
|
+
elif sys.argv[1] == "compile":
|
|
75
|
+
file = sys.argv[2]
|
|
76
|
+
name = "out.cvm"
|
|
77
|
+
run = False
|
|
78
|
+
dump = False
|
|
79
|
+
if len(sys.argv) >= 4:
|
|
80
|
+
if sys.argv[3] == "-o":
|
|
81
|
+
name = sys.argv[4]
|
|
82
|
+
elif sys.argv[3] == "-r":
|
|
83
|
+
run = True
|
|
84
|
+
if len(sys.argv) >= 5:
|
|
85
|
+
name = sys.argv[4]
|
|
86
|
+
elif sys.argv[3] == "--dump":
|
|
87
|
+
dump = True
|
|
88
|
+
|
|
89
|
+
compiled_code = []
|
|
90
|
+
|
|
91
|
+
with open(file, "r") as f:
|
|
92
|
+
lines = f.read().splitlines()
|
|
93
|
+
|
|
94
|
+
inComment = False
|
|
95
|
+
|
|
96
|
+
for i, line in enumerate(lines, start=1):
|
|
97
|
+
if inComment:
|
|
98
|
+
if line.endswith("*/"):
|
|
99
|
+
inComment = False
|
|
100
|
+
continue
|
|
101
|
+
|
|
102
|
+
if line.startswith("/*"):
|
|
103
|
+
if not line.endswith("*/"):
|
|
104
|
+
inComment = True
|
|
105
|
+
continue
|
|
106
|
+
|
|
107
|
+
if line.startswith("//"):
|
|
108
|
+
continue
|
|
109
|
+
|
|
110
|
+
try:
|
|
111
|
+
compiled_code.append(compile_line(line))
|
|
112
|
+
except Exception:
|
|
113
|
+
raise SyntaxError(f"at line {i} code is wrong")
|
|
114
|
+
|
|
115
|
+
with open(name, "w") as f:
|
|
116
|
+
f.write("\n".join(compiled_code))
|
|
117
|
+
|
|
118
|
+
if run:
|
|
119
|
+
vm.run("\n".join(compiled_code))
|
|
120
|
+
if dump:
|
|
121
|
+
print("\n".join(compiled_code))
|
|
122
|
+
|
|
123
|
+
elif sys.argv[1] == "run":
|
|
124
|
+
file = sys.argv[2]
|
|
125
|
+
flag = ""
|
|
126
|
+
showStack = False
|
|
127
|
+
if len(sys.argv) >= 4:
|
|
128
|
+
flag = sys.argv[3]
|
|
129
|
+
|
|
130
|
+
if flag == "--stack":
|
|
131
|
+
showStack = True
|
|
132
|
+
|
|
133
|
+
with open(file, "r") as f:
|
|
134
|
+
vm.run(f.read())
|
|
135
|
+
|
|
136
|
+
if showStack:
|
|
137
|
+
print(f"stack: {vm.stack}")
|
|
138
|
+
elif sys.argv[1] == "--version":
|
|
139
|
+
print(f"Circuit {version}")
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import random
|
|
2
|
+
class Vm:
|
|
3
|
+
|
|
4
|
+
def __init__(self):
|
|
5
|
+
self.stack = []
|
|
6
|
+
self.variables = {}
|
|
7
|
+
self.skip = False
|
|
8
|
+
def run_line(self, line):
|
|
9
|
+
if self.skip == True:
|
|
10
|
+
if line == "END":
|
|
11
|
+
self.skip = False
|
|
12
|
+
return
|
|
13
|
+
if line.startswith("PUSH "):
|
|
14
|
+
if ',' in line:
|
|
15
|
+
contents = line.replace("PUSH ", "").split(",")
|
|
16
|
+
for v in contents:
|
|
17
|
+
self.stack.append(v)
|
|
18
|
+
else:
|
|
19
|
+
self.stack.append(line[5:])
|
|
20
|
+
elif line == "PRINT":
|
|
21
|
+
if not self.stack:
|
|
22
|
+
raise RuntimeError("Stack Underflow")
|
|
23
|
+
print(self.stack.pop())
|
|
24
|
+
elif line == "INPUT":
|
|
25
|
+
self.stack.append(input())
|
|
26
|
+
elif line == "ADD":
|
|
27
|
+
b = str(self.stack.pop())
|
|
28
|
+
a = str(self.stack.pop())
|
|
29
|
+
if "." in a:
|
|
30
|
+
a = float(a)
|
|
31
|
+
else:
|
|
32
|
+
a = int(a)
|
|
33
|
+
if "." in b:
|
|
34
|
+
b = float(b)
|
|
35
|
+
else:
|
|
36
|
+
b = int(b)
|
|
37
|
+
self.stack.append(a + b)
|
|
38
|
+
elif line == "SUB":
|
|
39
|
+
b = str(self.stack.pop())
|
|
40
|
+
a = str(self.stack.pop())
|
|
41
|
+
if "." in a:
|
|
42
|
+
a = float(a)
|
|
43
|
+
else:
|
|
44
|
+
a = int(a)
|
|
45
|
+
if "." in b:
|
|
46
|
+
b = float(b)
|
|
47
|
+
else:
|
|
48
|
+
b = int(b)
|
|
49
|
+
self.stack.append(a - b)
|
|
50
|
+
elif line == "DIV":
|
|
51
|
+
b = str(self.stack.pop())
|
|
52
|
+
a = str(self.stack.pop())
|
|
53
|
+
if "." in a:
|
|
54
|
+
a = float(a)
|
|
55
|
+
else:
|
|
56
|
+
a = int(a)
|
|
57
|
+
if "." in b:
|
|
58
|
+
b = float(b)
|
|
59
|
+
else:
|
|
60
|
+
b = int(b)
|
|
61
|
+
self.stack.append(a / b)
|
|
62
|
+
elif line == "MUL":
|
|
63
|
+
b = str(self.stack.pop())
|
|
64
|
+
a = str(self.stack.pop())
|
|
65
|
+
if "." in a:
|
|
66
|
+
a = float(a)
|
|
67
|
+
else:
|
|
68
|
+
a = int(a)
|
|
69
|
+
if "." in b:
|
|
70
|
+
b = float(b)
|
|
71
|
+
else:
|
|
72
|
+
b = int(b)
|
|
73
|
+
self.stack.append(a * b)
|
|
74
|
+
elif line == "EQS":
|
|
75
|
+
b, a = str(self.stack.pop()), str(self.stack.pop())
|
|
76
|
+
if a == b:
|
|
77
|
+
self.stack.append("true")
|
|
78
|
+
else:
|
|
79
|
+
self.stack.append("false")
|
|
80
|
+
elif line.startswith("STORE "):
|
|
81
|
+
name = line[6:]
|
|
82
|
+
value = self.stack.pop()
|
|
83
|
+
self.variables[name] = value
|
|
84
|
+
elif line.startswith("LOAD "):
|
|
85
|
+
name = line[5:]
|
|
86
|
+
self.stack.append(self.variables[name])
|
|
87
|
+
elif line == "CONCAT":
|
|
88
|
+
b = str(self.stack.pop())
|
|
89
|
+
a = str(self.stack.pop())
|
|
90
|
+
self.stack.append(a + b)
|
|
91
|
+
elif line == "LT":
|
|
92
|
+
b, a = str(self.stack.pop()), self.stack.pop()
|
|
93
|
+
if "." in a:
|
|
94
|
+
a = float(a)
|
|
95
|
+
else:
|
|
96
|
+
a = int(a)
|
|
97
|
+
|
|
98
|
+
if "." in b:
|
|
99
|
+
b = float(b)
|
|
100
|
+
else:
|
|
101
|
+
b = int(b)
|
|
102
|
+
|
|
103
|
+
if a < b:
|
|
104
|
+
self.stack.append("true")
|
|
105
|
+
else:
|
|
106
|
+
self.stack.append("false")
|
|
107
|
+
elif line == "GT":
|
|
108
|
+
b, a = str(self.stack.pop()), self.stack.pop()
|
|
109
|
+
if "." in a:
|
|
110
|
+
a = float(a)
|
|
111
|
+
else:
|
|
112
|
+
a = int(a)
|
|
113
|
+
|
|
114
|
+
if "." in b:
|
|
115
|
+
b = float(b)
|
|
116
|
+
else:
|
|
117
|
+
b = int(b)
|
|
118
|
+
|
|
119
|
+
if a > b:
|
|
120
|
+
self.stack.append("true")
|
|
121
|
+
else:
|
|
122
|
+
self.stack.append("false")
|
|
123
|
+
elif line == "LTE":
|
|
124
|
+
b, a = str(self.stack.pop()), self.stack.pop()
|
|
125
|
+
if "." in a:
|
|
126
|
+
a = float(a)
|
|
127
|
+
else:
|
|
128
|
+
a = int(a)
|
|
129
|
+
|
|
130
|
+
if "." in b:
|
|
131
|
+
b = float(b)
|
|
132
|
+
else:
|
|
133
|
+
b = int(b)
|
|
134
|
+
|
|
135
|
+
if a <= b:
|
|
136
|
+
self.stack.append("true")
|
|
137
|
+
else:
|
|
138
|
+
self.stack.append("false")
|
|
139
|
+
elif line == "GTE":
|
|
140
|
+
b, a = str(self.stack.pop()), self.stack.pop()
|
|
141
|
+
if "." in a:
|
|
142
|
+
a = float(a)
|
|
143
|
+
else:
|
|
144
|
+
a = int(a)
|
|
145
|
+
|
|
146
|
+
if "." in b:
|
|
147
|
+
b = float(b)
|
|
148
|
+
else:
|
|
149
|
+
b = int(b)
|
|
150
|
+
|
|
151
|
+
if a >= b:
|
|
152
|
+
self.stack.append("true")
|
|
153
|
+
else:
|
|
154
|
+
self.stack.append("false")
|
|
155
|
+
elif line == "NEQ":
|
|
156
|
+
b, a = str(self.stack.pop()), self.stack.pop()
|
|
157
|
+
if a != b:
|
|
158
|
+
self.stack.append("true")
|
|
159
|
+
else:
|
|
160
|
+
self.stack.append("false")
|
|
161
|
+
elif line == "CLEAR":
|
|
162
|
+
self.stack.clear()
|
|
163
|
+
elif line == "ARR":
|
|
164
|
+
data = self.stack[:]
|
|
165
|
+
self.stack.clear()
|
|
166
|
+
newData = ",".join(data)
|
|
167
|
+
newData = f"{{{newData}}}"
|
|
168
|
+
self.stack.append(newData)
|
|
169
|
+
elif line == "IF":
|
|
170
|
+
boolean = self.stack.pop()
|
|
171
|
+
if boolean != "true":
|
|
172
|
+
self.skip = True
|
|
173
|
+
elif line == "END":
|
|
174
|
+
return
|
|
175
|
+
elif line.startswith("RAND "):
|
|
176
|
+
args = line.replace("RAND ", "").split(",")
|
|
177
|
+
minimum = int(args[0])
|
|
178
|
+
maximum = int(args[1])
|
|
179
|
+
self.stack.append(random.randint(minimum, maximum))
|
|
180
|
+
elif line == "PRINTNL":
|
|
181
|
+
if not self.stack:
|
|
182
|
+
raise RuntimeError("Stack Underflow")
|
|
183
|
+
print(self.stack.pop(), end="")
|
|
184
|
+
elif line == "DEL":
|
|
185
|
+
if not self.stack:
|
|
186
|
+
raise RuntimeError("Stack Underflow")
|
|
187
|
+
|
|
188
|
+
self.stack.pop()
|
|
189
|
+
else:
|
|
190
|
+
raise RuntimeError(f"invalid: {line}")
|
|
191
|
+
def run(self, code):
|
|
192
|
+
for line in code.splitlines():
|
|
193
|
+
if line.strip():
|
|
194
|
+
self.run_line(line)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: circuit-lang
|
|
3
|
+
Version: 0.5.0
|
|
4
|
+
Summary: The Circuit programming language and CVM.
|
|
5
|
+
Author: Olan
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: circuit,compiler,vm,cvm,stack-based
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
13
|
+
Requires-Python: >=3.8
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# Circuit!
|
|
17
|
+
Circuit 0.5-0 is out!
|
|
18
|
+
the public beta!
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
circuit/__init__.py
|
|
4
|
+
circuit/__main__.py
|
|
5
|
+
circuit/compiler.py
|
|
6
|
+
circuit/vm.py
|
|
7
|
+
circuit_lang.egg-info/PKG-INFO
|
|
8
|
+
circuit_lang.egg-info/SOURCES.txt
|
|
9
|
+
circuit_lang.egg-info/dependency_links.txt
|
|
10
|
+
circuit_lang.egg-info/entry_points.txt
|
|
11
|
+
circuit_lang.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
circuit
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "circuit-lang"
|
|
7
|
+
version = "0.5.0"
|
|
8
|
+
description = "The Circuit programming language and CVM."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Olan" }
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"circuit",
|
|
17
|
+
"compiler",
|
|
18
|
+
"vm",
|
|
19
|
+
"cvm",
|
|
20
|
+
"stack-based"
|
|
21
|
+
]
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Development Status :: 4 - Beta",
|
|
24
|
+
"Intended Audience :: Developers",
|
|
25
|
+
"Programming Language :: Python :: 3",
|
|
26
|
+
"Operating System :: OS Independent",
|
|
27
|
+
"Topic :: Software Development :: Compilers"
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.scripts]
|
|
31
|
+
circuit = "circuit.compiler:main"
|
|
32
|
+
|
|
33
|
+
[tool.setuptools]
|
|
34
|
+
packages = ["circuit"]
|