Sfoglia il codice sorgente

Add support for comments and a bit more documentation

Matt Coles 9 anni fa
parent
commit
0f8bd9ffe5
3 ha cambiato i file con 25 aggiunte e 6 eliminazioni
  1. 7 1
      README.md
  2. 11 2
      compiler.js
  3. 7 3
      example.mc

+ 7 - 1
README.md

@@ -3,7 +3,13 @@
3 3
 A basic compiler based off of @thejameskyle's super-tiny-compiler, compiles a simple LISP-esque syntax into runnable JS.
4 4
 
5 5
 Currently supports a few built-ins, `add`, `subtract`, `assign` and `log`. Hopefully these are self-explanatory, or at least 
6
-they should be from `example.mc`.
6
+they should be from `example.mc`. A `#` denotes that the rest of the line (until the compiler sees `\n`) as a comment and 
7
+means that it will not be compiled.
7 8
 
8 9
 The compiler runs like `node compiler.js file.mc` where `file.mc` is the file you wish to compile, and this will produce an
9 10
 `output.js` which requires `stdlib.js` to be in the same directory when running for now at least.
11
+
12
+Functions and variables are in different scopes, so variables can have the same names as functions - even builtins,
13
+thus making `(assign assign 5)` a totally okay thing to do.
14
+
15
+Note that this compiler is not only totally useless, but also horrendously inefficient. Either way it's a fun exercise :)

+ 11 - 2
compiler.js

@@ -17,9 +17,18 @@ var tokenizer = function (input) {
17 17
       pos++
18 18
       continue
19 19
     }
20
-    let whitespace = /\s/
20
+    let whitespace = /[#\s]/
21 21
     if (whitespace.test(char)) {
22
-      pos++
22
+      if (char === '#') {
23
+        comment = ''
24
+        while (char !== '\n') {
25
+          comment += char
26
+          char = input[++pos]
27
+        }
28
+        console.log("Ignoring comment: " + comment)
29
+      } else {
30
+        pos++
31
+      }
23 32
       continue
24 33
     }
25 34
     let numbers = /[0-9]/

+ 7 - 3
example.mc

@@ -1,3 +1,7 @@
1
-(assign twelve 12)
2
-(assign myvar (add twelve (subtract 6 2)))
3
-(log myvar)
1
+# This is a test program
2
+(assign twelve 12) # This assigns the variable twelve, to the number literal 12
3
+(assign myvar (add twelve (subtract 6 2))) # This assigns the variable myvar, to the result of adding the variable twelve to the result of subtracting 2 from 6
4
+(log myvar) # This logs the value of myvar
5
+(log 6) # This logs the string literal 6
6
+(assign twelve myvar) # This reassigns the variable twelve to the value of the variable myvar
7
+(log twelve) # This logs the new value of the variable twelve