Commits

Taegyoon Kim committed 894cd07

v0.3: defn, fn syntax changed; decl added

  • Participants
  • Parent commits 337c138

Comments (0)

Files changed (11)

 ; Project Euler 1
-(defn "void" problem1 ()
+(defn void problem1 ()
   (def sum 0)
   (for (def i 1) (< i 1000) (++ i)
     (if (or (== (% i 3) 0) (== (% i 5) 0)) (+= sum i)))
 (include "iostream" "vector")
 
-(defn "std::vector<int>" primesUnder ("int limit")
-  (code "std::vector<bool> p(limit)")
+(defn std::vector<int> primesUnder (int limit)
+  (decl std::vector<bool> |p(limit)|)
   (if (>= limit 3)
     (= (at p 2) true))
   (for (def i 3) (< i limit) (+= i 2)
       (continue))
     (for (def j (* i 2)) (< j limit) (+= j i)
       (= (at p j) false)))  
-  (code "std::vector<int> r")
+  (decl std::vector<int> r)
   (for (def i 2) (< i limit) (++ i)
     (if (at p i)
       (r.push_back i)))
 (include "stdio.h")
-(defn "int" main ()
+(defn int main ()
   (def a 1 b 1 s 0)
   (while 1
     (def c (+ a b))
 (include "stdio.h")
-(defn "int" main ()
+(defn int main ()
   (def num 600851475143 p 2)
   (while true
     (while (== 0 (% num p))
 ; Project Euler 4
 (include "cstdio" "cstring")
     
-(defn "bool" isPalindrome ("int p")
-  (code "char ps[10]")
+(defn bool isPalindrome (int p)  
+  (decl char |ps[10]|)
   (std::sprintf ps "%d" p)
   (def len (std::strlen ps)
     to (/ len 2)
 ; Project Euler 5
-(include "cstdio")
-    
-(defn "int" main ()
+(main
   (def n 20)
   (for (def i 19) (>= i 2) (-- i)
     (when (!= 0 (% n i)) (+= n 20) (= i 20)))
-  (std::printf "%d\n" n))
+  (prn n))
 (include "cstdio")
 
-(defn "int" main ()
+(main
   (def sumN 0 sqSum 0)
   (for (def i 1) (<= i 100) (++ i)
     (+= sumN i)
-(include "cstdio")
-
-(defn "bool" isPrime ("int n")
+(defn bool isPrime (int n)
   (for (def i 2) (<= (* i i) n) (++ i)
     (if (== 0 (% n i)) (return false)))
   (return true))
   
-(defn "int" main ()
+(main
   (def count 0 i 2)
   (while true
     (when (isPrime i)
       (++ count)
       (when (== count 10001)
-        (std::printf "%d\n" i)
+        (prn i)
         (break)))
     (++ i)))
 (include "cstdio")
 
-(defn "int" main ()
+(main
   (for (def a 1) (< a 1000) (++ a)
     (for (def b (+ a 1)) (< b 1000) (++ b)
       (def c (- 1000 a b))
 (main
-  (def d (fn ("double x") (return (- (* x x) 0.5))))
-  (def adder (fn ("int x") (return (fn ("int y") (return (+ x y))))))
+  (def d (fn (double x) (return (- (* x x) 0.5))))
+  (def adder (fn (int x) (return (fn (int y) (return (+ x y))))))
   (def add1 (adder 1))
   (prn "hello world")
   (prn (d 2.0))
 ; (C) 2014 KIM Taegyoon
 (require compatibility/defmacro)
 
-(define version "0.2.8")
+(define version "0.3")
 (define (readline)
   (read-line (current-input-port) 'any))
 
                  [(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) (format "~a ~a(~a) {\n~a;}\n" (list-ref e 1) (list-ref e 2) (string-join (list-ref e 3) ",") (string-join (map compile-expr (drop e 4)) ";\n"))]
+                 ; (defn int main (int argc char *argv[]) (return 0))
+                 [(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")]
+                 ; (decl char s[10] "") => char s[10]="" ; declares a variable
+                 [(decl) (~a (second e) " " (third e) (if (<= (length e) 3) "" (~a "=" (compile-expr (fourth e)))))]
                  ; (+ A B C ...) => (A + B + C + ...)
                  [(+ - * / << >>) (string-join (map compile-expr (rest e)) (~a f) #:before-first "(" #:after-last ")")]
                  ; (++ A) => (++ A) ; unary operators
                  [(case) (string-join (for/list ([x (rest e)]) (format "case ~a:" x)))]
                  ; (default) => default:
                  [(default) "default:"]
-                 ; (fn ("int a" "int b") (return (+ a b))) => [&](int a, int b) {return a + b;}
-                 [(fn) (format "[&](~a) {\n~a;}" (string-join (second e) ",") (string-join (map compile-expr (drop e 2)) ";\n"))]
+                 ; (fn (int a int b) (return (+ a b))) => [&](int a, int b) {return a + b;}
+                 [(fn) (format "[&](~a) {\n~a;}" (let [(args (second 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 2)) ";\n"))]
                  ; (code "CODE") => CODE as-is
                  [(code) (~a (second e))]
                  ; (format form ...) ; compile-time formatting