Coder Social home page Coder Social logo

calcium-lang's Introduction

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.

calcium-lang's People

Contributors

0xcaf2 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.