0.14.1 • Published 2 years ago

calcium-lang v0.14.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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.
0.14.1

2 years ago

0.14.0

2 years ago

0.10.1

2 years ago

0.6.3

2 years ago

0.12.0

2 years ago

0.6.2

2 years ago

0.13.0

2 years ago

0.13.1

2 years ago

0.6.4

2 years ago

0.5.0

2 years ago

0.4.1

2 years ago

0.13.3

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.4.0

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago