A transpiler for a subset of Swift. Transpiles to JavaScript.
- Lexer
- AST Printer
- Parser
- JavaScript Code Generation
- REPL
var,let, variables and assignmentsintliterals- classes
- functions
- operators
+,-,*,/ - parenthesis and brackets
(,),{,} print- optional semicolons
; - Constructs an AST and prints it in human-readable form
- Generates JS (Javascript) code from Swift (subset)
- Lexer
- Parser
- CodeGen
- General error recovery
- A
REPLloop
Usage: compiler <command>
Commands:
build Compile all .swift files in current directory to .js
repl Run interactive REPL
pipe Read all data from standard input
--help Show the help message
swift buildswift runswift test// run unit tests
./compiler repl
You can run commands such as:
echo "print 1 + 2 * (3 + 4);" | ./compiler pipe
which will output:
=== AST ===
PrintStmt
BinaryExpr(plus)
IntLiteral(1)
BinaryExpr(star)
IntLiteral(2)
BinaryExpr(plus)
IntLiteral(3)
IntLiteral(4)
=== Generated JS ===
console.log((1 + (2 * (3 + 4))));
cat testcode.swift | ./compiler pipe
which will output:
=== AST ===
PrintStmt
BinaryExpr(plus)
IntLiteral(1)
BinaryExpr(star)
IntLiteral(2)
BinaryExpr(plus)
IntLiteral(3)
IntLiteral(4)
=== Generated JS ===
console.log((1 + (2 * (3 + 4))));