update scriptish

pull/21/head
Matthew Butterick 6 years ago
parent 944c543db7
commit e0944fea24

@ -1,9 +1,19 @@
#lang br/quicklang
#lang br
(require racket/stxparam)
(provide (all-defined-out))
(provide (all-defined-out)
#%top-interaction #%top
(rename-out [my-datum #%datum]))
(define-macro top #'#%module-begin)
(define-macro (my-datum . VAL)
(with-syntax ([NEW-VAL (let ([val (syntax->datum #'VAL)])
(if (and (integer? val)
(inexact? val))
(inexact->exact val)
val))])
#'(#%datum . NEW-VAL)))
(define-macro (var ID VAL) #'(define ID VAL))
(define (add/concat . xs)
@ -12,8 +22,14 @@
[(ormap string? xs) (string-join (map ~a xs) "")]))
(define-macro-cases add-or-sub
[(_ VAL) #'VAL]
[(_ . VALS) #'(add/concat . VALS)])
[(_ LEFT "+" RIGHT) #'(add/concat LEFT RIGHT)]
[(_ LEFT "-" RIGHT) #'(- LEFT RIGHT)]
[(_ OTHER) #'OTHER])
(define-macro-cases mult-or-div
[(_ LEFT "*" RIGHT) #'(* LEFT RIGHT)]
[(_ LEFT "/" RIGHT) #'(/ LEFT RIGHT)]
[(_ OTHER) #'OTHER])
(define-macro (object (K V) ...)
#'(make-hash (list (cons K V) ...)))

@ -5,8 +5,9 @@ statement : (var | expr | return) /";" | if | while
var : /"var" id /"=" expr
@expr : comparison
comparison : [comparison ("!=" | "==")] add-or-sub
add-or-sub : [@add-or-sub /"+"] value
@value : id | INTEGER | STRING | object
add-or-sub : [add-or-sub ("+" | "-")] mult-or-div
mult-or-div : [mult-or-div ("*" | "/")] value
@value : id | NUMBER | STRING | object
| fun | app | increment
increment : id /"++"
object : /"{" @kvs /"}"

@ -5,10 +5,13 @@
(provide read-syntax))
(define-lex-abbrev reserved-terms
(:or "var" "=" ";" "+" "{" "}" "'" "\""
(:or "var" "=" ";" "+" "*" "/"
"-" "{" "}" "'" "\""
":" "," "(" ")" "//" "/*" "*/"
"if" "else" "while" "==" "!=" "function" "return" "++"))
(define-lex-abbrev digits (:+ (char-set "0123456789")))
(define tokenize-1
(lexer-srcloc
[(:or (from/stop-before "//" "\n")
@ -18,8 +21,9 @@
(token 'ID (string->symbol lexeme))]
[(:+ (:- (:or alphabetic punctuation) reserved-terms))
(token 'DEREF (map string->symbol (string-split lexeme ".")))]
[(:+ (char-set "0123456789"))
(token 'INTEGER (string->number lexeme))]
[(:seq (:? "-") (:or (:seq (:? digits) "." digits)
(:seq digits (:? "."))))
(token 'NUMBER (string->number lexeme))]
[(:or (from/to "\"" "\"") (from/to "'" "'"))
(token 'STRING (string-trim lexeme (substring lexeme 0 1)))]
[whitespace (token 'WHITE #:skip? #t)]

@ -0,0 +1,21 @@
#lang scriptish-demo
-1; // line comment
10 ;
/* multi
// line
comment */
2;
var al = -.123;
var bo = "foo";
var cy = 1.;
var da = 'bar';
al;bo;cy;da;
cy + cy + cy;
al+cy*al-cy/100;
cy++;
cy + cy + bo + da;
bo+cy+cy+da;
Loading…
Cancel
Save