calcium-lang
v0.14.1
Published
Calcium language interpreter
Downloads
16
Readme
calcium-lang
Calcium language interpreter
OBSOLETE
This project was forked to calcium-js and calcium-py.
Calcium uses JSON-based code as input.
const code = [
[1, [], "#", "0_21"],
[1, [], "expr", ["call", ["var", "print"], ["Hello, World."]]],
[1, [], "end"],
];
prints "Hello, World!".
Calcium supports basic statements such as if
, while
, functions, and class definition. See here.
Python's subset code can be translated to Calcium code.
There is a script which reads a Python program and generates a JSON array. For example,
def is_remainder_zero(x, y):
r = (x % y) == 0
return r
prime = []
for i in range(101):
j = 2
while True:
if j >= i:
break
if is_remainder_zero(i, j):
break
else:
j += 1
if j == i:
prime.append(i)
result = prime
print(
str(result)
== "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]"
)
will be translated to:
[
[1, [], "#", "0_21"],
[1, [], "def", "is_remainder_zero", ["x", "y"]],
[2, [], "=", ["var", "r"], ["==", ["%", ["var", "x"], ["var", "y"]], 0]],
[2, [], "return", ["var", "r"]],
[1, [], "=", ["var", "prime"], [[]]],
[1, [], "for", ["var", "i"], ["call", ["var", "range"], [101]]],
[2, [], "=", ["var", "j"], 2],
[2, [], "while", true],
[3, [], "ifs"],
[4, [], "if", [">=", ["var", "j"], ["var", "i"]]],
[5, [], "break"],
[3, [], "ifs"],
[4, [], "if", ["call", ["var", "is_remainder_zero"], [["var", "i"], ["var", "j"]]]],
[5, [], "break"],
[4, [], "else"],
[5, [], "+=", ["var", "j"], 1],
[2, [], "ifs"],
[3, [], "if", ["==", ["var", "j"], ["var", "i"]]],
[4, [], "expr", ["call", ["attr", "prime", "append"], [["var", "i"]]]],
[1, [], "=", ["var", "result"], ["var", "prime"]],
[1, [], "expr", ["call", ["var", "print"], [["==", ["call", ["var", "str"], [["var", "result"]]], "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]"]]]],
[1, [], "end"]
]
Basically Calcium's one line corresponds to Python's one.
The Calcium engine can be embedded in a Web page or a WebView.
import * as Calcium from "calcium-lang";
const runtime = new Calcium.Runtime(code); // code should be a JSON array.
creates the runtime. To output from print function, set a callback as:
runtime.setOutputFunction((desc) => console.log(desc));
To execute the code, invoke run()
method.
runtime.run(); // Run the code.