{"id":18447912,"url":"https://github.com/justinsdk/toy_lang","last_synced_at":"2025-10-07T23:08:45.156Z","repository":{"id":152420920,"uuid":"130933678","full_name":"JustinSDK/toy_lang","owner":"JustinSDK","description":"The first language I made.","archived":false,"fork":false,"pushed_at":"2018-09-05T01:12:33.000Z","size":718,"stargazers_count":30,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T01:34:19.450Z","etag":null,"topics":["ecmascript6","es6","interpreter","javascript","object-oriented-programming","parser","programming-languages","prototype","tokenizer"],"latest_commit_sha":null,"homepage":"https://openhome.cc/Gossip/Toy/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JustinSDK.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-25T01:30:35.000Z","updated_at":"2025-01-07T02:08:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"8722c1c1-c78b-41da-ac44-50b2248e9779","html_url":"https://github.com/JustinSDK/toy_lang","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/JustinSDK/toy_lang","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JustinSDK%2Ftoy_lang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JustinSDK%2Ftoy_lang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JustinSDK%2Ftoy_lang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JustinSDK%2Ftoy_lang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JustinSDK","download_url":"https://codeload.github.com/JustinSDK/toy_lang/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JustinSDK%2Ftoy_lang/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270807934,"owners_count":24649346,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-17T02:00:09.016Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ecmascript6","es6","interpreter","javascript","object-oriented-programming","parser","programming-languages","prototype","tokenizer"],"created_at":"2024-11-06T07:14:41.499Z","updated_at":"2025-10-07T23:08:40.133Z","avatar_url":"https://github.com/JustinSDK.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Toy Lang\n\nToy lang was started from a [gist](https://gist.github.com/JustinSDK/9c38136b90137387ad3518d4e99d15ba). It's the first language I made. ES6 modules support is required.\n\n- Keywords: `if`, `else`, `while`, `def`, `return`, `and`, `or`, `not`, `new`, `class`, `this`, `arguments`, `throw`, `try`, `catch`, `nonlocal`, `switch`, `case`, `default`, `break`, `import`, `as`, `from`\n- Literals: 3.14 (number), `true`, `false`, `'Hello, World'` (string), `\\r`, `'\\n'`, `'\\t'`, `'\\\\'`, `'\\''`\n- Operators: `new`, `.`, `==`, `!=`, `\u003e=`, `\u003e`, `\u003c=`, `\u003c`, `and`, `or`, `not`, `+`, `-`, `*`, `/`, `%`, `\u0026`, `|`, `^`, `\u003c\u003c`, `\u003e\u003e`\n- Assigns: `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `\u0026=`, `|=`, `^=`, `\u003c\u003c=`, `\u003e\u003e=`\n- Built-in functions: `input`, `print`, `println`, `hasValue`, `noValue`, `range`, `iterate`, `typeof`, `isInstance`\n- Built-in classes: `Object`, `Module`, `Class`, `Function`, `Number`, `String`, `List`, `Traceable`, `Exception`\n- Comment: `#`\n\n[Play It](https://openhome.cc/Gossip/Computation/toy_lang/)\n\n# Examples ([More examples](https://github.com/JustinSDK/toy_lang/tree/master/toy_lang/examples))\n\n- Hello World\n\n```python\n# Is a language easier if 'Hello World' is easier?\n\nimport '/lib/toy'\n\nprintln('Hello World')\nprintln('Hello {0}'.format('World'))\nprintln(String.format('Hello {0}', 'World'))\ntoy.hello()\n```\n\n- Multiplication Table\n\n```python\ndef print_row(n) {\n    i = 2\n    while i \u003c 10 {\n        print('{0}*{1}={2}\\t'.format(i, n, i * n))\n        i += 1 \n    }\n    println()\n}\n\niterate(1, 10).forEach(print_row)\n```\n\n- Tower of Hanoi\n\n```python\ndef hanoi(n, a, b, c) {\n    if n == 1 {\n        println('Move sheet from {0} to {1}'.format(a , c))\n    } \n    else {\n        hanoi(n - 1, a, c, b)\n        hanoi(1, a, b, c)\n        hanoi(n - 1, b, a, c) \n    }\n}\n\nhanoi(3, 'A', 'B', 'C')\n```\n\n- Factorial\n\n```Java\ndef factorial(n) {\n    if n == 0 {\n        return 1\n    }\n\n    return n * factorial(n - 1)\n}\n\n# use \\ to wrap long lines\nrange(1, 6).map(n -\u003e '{0}! = {1}'.format(n, factorial(n))) \\\n           .forEach(println)\n```\n\n- Tricolour \n\n```java\ndef adjust(flags) {\n    b = 0\n    w = 0\n    r = flags.length() - 1\n    while flags.get(w) == 'B' and w \u003c flags.length() {\n        w += 1\n    }\n    while flags.get(r) == 'R' and r \u003e 0 {\n        r -= 1\n    }\n    while w \u003c= r {\n        switch flags.get(w) {\n            case 'W'\n                w += 1\n            case 'B'\n                flags.swap(b, w)\n                w += 1\n                b += 1\n            default\n                flags.swap(r, w)\n                r -= 1\n        }\n    }\n    return flags\n}\n\nflags = 'RWBBWRWR'.split('')\nprintln(adjust(flags))\n```\n\n- Class\n\n```java\nclass AccountException(Exception) {\n    def init() {\n        this.super(Exception, 'init', arguments)\n    }\n}\n\nclass Account {\n    # it's a field\n    balance = 0 \n\n    def init(number, name) {\n        this.number = number\n        this.name = name\n    }\n\n    def deposit(amount) {\n        if amount \u003c= 0 {\n            throw new AccountException('must be positive')\n        }\n\n        this.balance += amount\n    }\n\n    def toString() {\n        return '{0}, {1}, {2}'.format(this.number, this.name, this.balance)\n    }\n}\n\nacct = new Account('123', 'Justin')\nacct.deposit(100)\n\nprintln(acct)\n\ntry {\n    acct.deposit(-100)\n}\ncatch e {\n    e.printStackTrace()\n}\n```\n\n- Built-in Classes\n\n```java\ndef sum(lt) {\n    return 0 if lt.isEmpty() else (lt.get(0) + sum(lt.slice(1)))\n}\n    \nlt = range(1, 11)\nprintln('{0}={1}'.format(lt.join('+'), sum(lt)))\n\nprintln((new String('aBc')).toUpperCase())\nprintln((new String('aBc')).toLowerCase())\n```\n\n- Closure\n\n```javascript\ndef foo() {\n    x = 10\n    def inner(y) {\n        # closure closes the variable x, not its value\n        return x + y\n    }\n    x = 30 \n    return inner\n}\n\nf = foo()\nprintln('f(20) is ' + f(20))\n\ndef orz() {\n    x = 10\n    class Inner {\n        y = x\n        def init(p) {\n            # closure closes the variable x, not its value\n            this.z = x + p\n        }\n\n        def x() {\n            # closure closes the variable x, not its value\n            return x\n        }\n    }\n    x = 30\n    return Inner\n}\n\nclz = orz()\nobj = new clz(20)\n\nprintln('obj.x() is ' + obj.x())\nprintln('obj.y is ' + obj.y)\nprintln('obj.z is ' + obj.z)\n\ndef foo2(x) {\n    def getX() {\n        return x\n    }\n\n    def setX(v) {\n        nonlocal x = v\n    }\n\n    return [getX, setX]\n}\n\naccessor = foo2(10)\ngetX = accessor.get(0)\nsetX = accessor.get(1)\n\nprintln(getX()) \nsetX(100)\nprintln(getX())\n```\n\n- Lambda expression\n\n```java    \nmax = (n1, n2) -\u003e n1 if n1 \u003e n2 else n2\nprintln(max(10, 20))\n\n# you may also use parentheses to wrap wrapping long lines\n([1, 2, 3, 4, 5].filter(elem -\u003e elem \u003e= 2) \n                .map(elem -\u003e elem * 100)   \n                .forEach(println))\n\n(range(1, 10)\n  .map(n -\u003e range(2, 10).map(i -\u003e '{0}*{1}={2}'.format(i, n, i * n)).join('\\t'))\n  .forEach(println))\n\ndef foo3(x, y) {\n    return () -\u003e x + y\n}\n\nprintln(foo3(10, 20)())\n\ndef orz() {\n    x = 10\n    return (y, z) -\u003e x + y + z \n}\n\nprintln(orz()(100, 200))\n\n# IIFE\n(() -\u003e println('XD'))()\n(x -\u003e println(x))('XD')\nprintln(((x, y) -\u003e x + y)(1, 2))\n```\n\n- Mixin\n\n```ruby\nclass Ordered {\n    def lessThan(that) {\n        return this.compare(that) \u003c 0\n    }\n\n    def lessEqualsThan(that) {\n        return this.lessThan(that) or this.equals(that)\n    }\n\n    def greaterThan(that) {\n        return not this.lessEqualsThan(that)\n    }\n\n    def greaterEqualsThan(that) {\n        return not this.lessThan(that)\n    }\n}\n\nclass Circle {\n    def init(radius) {\n        this.radius = radius\n    }\n\n    def compare(that) {\n        return this.radius - that.radius\n    }\n\n    def equals(that) {\n        return this.radius == that.radius\n    }\n}\n\nCircle.mixin(Ordered)\n\nc1 = new Circle(10)\nc2 = new Circle(20)\n\nprintln(c1.lessThan(c2))\nprintln(c1.lessEqualsThan(c2))\nprintln(c1.greaterThan(c2))\nprintln(c1.greaterEqualsThan(c2))\n```\n\n- Inheritance 1\n\n```python\nclass PA {\n    def init() {\n        println('PA init')\n    }\n\n    def ma(x, y) {\n        println(x)\n        println(y)\n    }\n}\n\nclass PB {\n    def mb() {\n        println('mb')\n    }\n}\n\nclass C(PA, PB) {\n    def init() {\n        this.super(PA, 'init')\n        println('C init')\n    }\n\n    def mc() {\n        println('mc')\n    }\n\n    def ma(x, y) {\n        this.super(PA, 'ma', arguments)\n        println('c.ma()')\n    }\n}\n\nc = new C()\nc.ma(10, 20)\nc.mb()\nc.mc()\n```\n\n- Inheritance 2\n\n```python\nclass Ordered {\n    def lessThan(that) {\n        return this.compare(that) \u003c 0\n    }\n\n    def lessEqualsThan(that) {\n        return this.lessThan(that) or this.equals(that)\n    }\n\n    def greaterThan(that) {\n        return not this.lessEqualsThan(that)\n    }\n\n    def greaterEqualsThan(that) {\n        return not this.lessThan(that)\n    }\n}\n\nclass Circle(Ordered) {\n    def init(radius) {\n        this.radius = radius\n    }\n\n    def compare(that) {\n        return this.radius - that.radius\n    }\n\n    def equals(that) {\n        return this.radius == that.radius\n    }\n}\n\nc1 = new Circle(10)\nc2 = new Circle(20)\n\nprintln(c1.lessThan(c2))\nprintln(c1.lessEqualsThan(c2))\nprintln(c1.greaterThan(c2))\nprintln(c1.greaterEqualsThan(c2))\n```\n\n- meta programming 1\n\n```javascript\nprintln(Object.ownMethods())\nObject.deleteOwnMethod('toString')    \nprintln(Object.ownMethods())\n\ndef toString() {\n    props = this.ownProperties()\n    # each prop is a List instance which contains name and value\n    return  props.map(prop -\u003e prop.join()).join('\\n')\n}\n\no1 = new Object()\no1.x = 1\no1.y = 2\no1.z = 3\nprintln(toString.apply(o1))\n\n(o2 = new Object([\n    ['x', 10], \n    ['y', 20]\n]))\nprintln(toString.apply(o2))\n\ndef foo4(p) {\n    return this.x + this.y + this.z + p\n}\n\n# The 2nd parameter of the apply method accepts a List instance. \nprintln(foo4.apply(o1, [40]))\n```\n\n- meta programming 2\n\n```javascript\nclass PA {\n    def pa() {\n        println('pa')\n    }\n}\n\nclass PB {\n    def pb() {\n        println('pb')\n    }\n}\n\nclass C {\n    def c() {\n        println('c') \n    }\n}\n\nprintln(C.parents())\nC.parents([PA, PB])\nprintln(C.parents())\n\n(new C()).pa()\n(new C()).pb()\n(new C()).c()\n\ndef toString() {\n    return this.class().name()\n}\n\nOrz = new Class('Orz', [PA, PB], [toString])\norz = new Orz()\norz.pa()\norz.pb()\nprintln(orz)\n```\n\n----------\n\n[openhome.cc](https://openhome.cc)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustinsdk%2Ftoy_lang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjustinsdk%2Ftoy_lang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjustinsdk%2Ftoy_lang/lists"}