1. Taegyoon Kim
  2. L++

Commits

Taegyoon Kim  committed 8e208e3

syntax and semantics

  • Participants
  • Parent commits f5dc5c0
  • Branches master

Comments (0)

Files changed (2)

File README.md

View file
  • Ignore whitespace
 L++ is a programming language that transcompiles to C++. It uses Lisp-like syntax.
 
 ## Syntax and semantics ##
-See [the source code](https://bitbucket.org/ktg/l/src).
+`(define-syntax ...)` ; defines a macro
+
+`(define-syntax-rule (id . pattern) template)` ; defines a macro
+
+`(defmacro id formals body ...+)` ; defines a (non-hygienic) macro id through a procedure that manipulates S-expressions, as opposed to syntax objects.
+
+`(include "file1.h" ...)` => `#include "file1.h" ...`
+
+`(defn int main (int argc char **argv) (...))` => `int main(int argc, char **argv) {...}`
+
+`(def a 3 b 4.0 ...)` => `auto a = 3; auto b = 4.0; ...`
+
+`(decl char s[10] "")` => `char s[10]=""` ; declares a variable
+
+`(+ A B C ...)` => `(A + B + C + ...)` (`+ - * / << >>`)
+
+`(++ A)` => `(++ A)` ; unary operators (`++ -- not compl`)
+
+`(< A B)` => `(A < B)` ; binary operators (`< <= > >= == != % = += -= *= /= %= and and_eq bitand bitor not_eq or or_eq xor xor_eq`)
+
+`(return A)` => `return A`
+
+`(? TEST THEN ELSE)` => `(TEST ? THEN : ELSE)`
+
+`(if TEST THEN [ELSE])` => `if (TEST) THEN; [else ELSE]`
+
+`(when TEST THEN ...)` => `if (TEST) {THEN; ...;}`
+
+`(while TEST BODY ...)` => `while (TEST) {BODY; ...;}`
+
+`(for INIT TEST STEP BODY ...)` => `for (INIT; TEST; STEP) {BODY; ...;}`
+
+`(foreach VAR CONTAINER BODY ...)` => `for (auto &VAR : CONTAINER) {BODY; ...;}`
+
+`(do BODY ...)` => `{BODY; ...;}`
+
+`(do/e EXPR ...)` => `(EXPR, ...)`
+
+`(at ARRAY POSITION)` => `ARRAY[POSITION]`
+
+`(break)` => `break` (`break continue`)
+
+`(main BODY ...)` => `int main(int argc, char **argv) {BODY; ...; return 0;}`
+
+`(pr A ...)` => `std::cout << A << ...`
+
+`(prn A ...)` => `std::cout << A << ... << std::endl`
+
+`(label ID)` => `ID:`
+
+`(goto ID)` => `goto ID`
+
+`(switch EXPR BODY ...)` => `switch (EXPR) {BODY; ...;}`
+
+`(case EXPR ...)` => `case EXPR: case ...:`
+
+`(default)` => `default:`
+
+`(fn (int a int b) (return (+ a b)))` => `[&](int a, int b) {return a + b;}`
+
+`(code "CODE")` => `CODE` as-is
+
+`(format form ...)` ; compile-time formatting
+
+`(F ARG ...)` => `F(ARG, ...)`
+
+See [the source code](https://bitbucket.org/ktg/l/src) for details.
 
 ### Comments ###
 `;` `#!` end-of-line comment

File l++.rkt

View file
  • Ignore whitespace
                  [(defmacro) (set-add! macros (second e)) (eval e ns) ""]
                  ; (include "file1.h" ...) => #include "file1.h" ...
                  [(include) (string-join (for/list ([x (rest e)]) (format "#include ~s\n" x)) "")]
-                 ; (defn int main (int argc char *argv[]) (return 0))
+                 ; (defn int main (int argc char **argv) (...)) => int main(int argc, char **argv) {...}
                  [(defn) (format "~a ~a(~a) {\n~a;}\n" (list-ref e 1) (list-ref e 2) (let [(args (fourth e))] (string-join (for/list ([i (in-range 0 (length args) 2)]) (format "~a ~a" (list-ref args i) (list-ref args (add1 i)))) ",")) (string-join (map compile-expr (drop e 4)) ";\n"))]
                  ; (def a 3 b 4.0 ...) => auto a = 3; auto b = 4.0; ...
                  [(def) (string-join (for/list ([i (in-range 1 (length e) 2)]) (format "auto ~a=~a" (list-ref e i) (compile-expr (list-ref e (add1 i))))) ";\n")]