~xigoi/xongue

48a96ebe5cf631287582dffe60a0e9d7f8e13b83 — Adam Blažek 5 months ago d053029
README
3 files changed, 81 insertions(+), 3 deletions(-)

A README.md
M fizzbuzz.c.xo
A javascript.lua
A README.md => README.md +52 -0
@@ 0,0 1,52 @@
# 👅 xongue

Tired of every language having a different syntax which you have to remember and pay attention to? Do you want to focus on the code logic rather than playing around with characters? Then _xongue_ might be for you!

_xongue_ is a Lua library that allows easily creating transpilers (source-to-source compilers) from a family of related languages (tongues) that can target any programming language.

Currently it's just an experiment, but I'm planning to eventually turn it into a fully usable tool.

## Example

Create and use a transpiler for a tiny subset of JavaScript like this:

```lua
local xongue = require "xongue"

local js_xongue = xongue.new {
  -- Keywords that will produce syntactic forms in the target language
  keywords = {
    var = function(name, init)
      return string.format("let %s = %s", name, init)
    end,
    ["*"] = function(x, y)
      return string.format("(%s * %s)", x, y)
    end,
  },
  -- What to do with a call that isn't a keyword
  call = function(name, args)
    return string.format("%s(%s)", name, xongue.concat(args, ", "))
  end,
  -- How to join top-level statements together
  top_level = function(stmts)
    return xongue.concat(stmts, ";\n")
  end,
}

local code = [[
  (var six 6)
  (var seven 7)
  (var answer (* six seven))
  (console.log answer)
]]
print(js_xongue:transpile(code))
```

Output:

```javascript
let six = 6;
let seven = 7;
let answer = six * seven;
console.log(answer);
```

M fizzbuzz.c.xo => fizzbuzz.c.xo +3 -3
@@ 3,12 3,12 @@
  (if (divisible n 3)
    (if (divisible n 5)
      (printf "FizzBuzz\n")
    else
      else
      (printf "Fizz\n"))
  else
    else
    (if (divisible n 5)
      (printf "Buzz\n")
    else
      else
      (printf "%d\n" n))))
(main
  (for..< n 1 101

A javascript.lua => javascript.lua +26 -0
@@ 0,0 1,26 @@
local xongue = require "xongue"

local js_xongue = xongue.new {
  call = function(name, args)
    return string.format("%s(%s)", name, xongue.concat(args, ", "))
  end,
  keywords = {
    var = function(name, init)
      return string.format("let %s = %s", name, init)
    end,
    ["*"] = function(x, y)
      return string.format("(%s * %s)", x, y)
    end,
  },
  top_level = function(stmts)
    return xongue.concat(stmts, ";\n")
  end,
}

local code = [[
  (var six 6)
  (var seven 7)
  (var answer (* six seven))
  (console.log answer)
]]
print(js_xongue:transpile(code))