Added support for piping files into tigerscheme.
This commit is contained in:
parent
96cc19b207
commit
edadfea781
47
example.scm
Normal file
47
example.scm
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
; Test functions
|
||||||
|
|
||||||
|
(define add-three (lambda (x y z) (+ (+ x z) y)))
|
||||||
|
|
||||||
|
; Test factorial
|
||||||
|
|
||||||
|
(define fac (lambda (x) (if (>= x 1) (* x (fac (+ x -1))) 1)))
|
||||||
|
|
||||||
|
(define (faca x a)
|
||||||
|
(if (>= x 1)
|
||||||
|
(faca (+ x -1) (* x a))
|
||||||
|
a))
|
||||||
|
|
||||||
|
(define x 5)
|
||||||
|
(display x) (newline)
|
||||||
|
(display (fac x)) (newline)
|
||||||
|
(display (faca x 1)) (newline)
|
||||||
|
|
||||||
|
; Test stuff?
|
||||||
|
|
||||||
|
(define (f . l) (cdr l))
|
||||||
|
(define y '(1 2 3))
|
||||||
|
|
||||||
|
(display y) (newline)
|
||||||
|
(display (car y)) (newline)
|
||||||
|
(display (cdr y)) (newline)
|
||||||
|
|
||||||
|
; Check set!
|
||||||
|
|
||||||
|
(set! x 10)
|
||||||
|
(display x) (newline)
|
||||||
|
|
||||||
|
; Test syntax-rules
|
||||||
|
|
||||||
|
(define-syntax and
|
||||||
|
(syntax-rules ()
|
||||||
|
((and) #t)
|
||||||
|
((and test) test)
|
||||||
|
((and test1 test2 ...)
|
||||||
|
(if test1 (and test2 ...) #f))))
|
||||||
|
|
||||||
|
(display (and #t #t)) (newline)
|
||||||
|
(display (and #f #t)) (newline)
|
||||||
|
(display (and #f #f)) (newline)
|
||||||
|
(display (and #t #f)) (newline)
|
||||||
|
|
|
@ -262,6 +262,11 @@ let /* Booleans */
|
||||||
in ascii = 41
|
in ascii = 41
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function is_comment_start (index: int): int =
|
||||||
|
let var ascii := ord(substring(str, index, 1))
|
||||||
|
in ascii = ord(";")
|
||||||
|
end
|
||||||
|
|
||||||
function is_tick (index: int): int =
|
function is_tick (index: int): int =
|
||||||
let var ascii := ord(substring(str, index, 1))
|
let var ascii := ord(substring(str, index, 1))
|
||||||
in ascii = ord("'")
|
in ascii = ord("'")
|
||||||
|
@ -320,8 +325,16 @@ let /* Booleans */
|
||||||
, pos_r = pos_r }
|
, pos_r = pos_r }
|
||||||
|
|
||||||
function ignore_ws () =
|
function ignore_ws () =
|
||||||
while index < size(str) & is_ws(index)
|
/* Ignore whitespace */
|
||||||
do index := index + 1
|
if index < size(str) & is_ws(index)
|
||||||
|
then (index := index + 1; ignore_ws())
|
||||||
|
|
||||||
|
/* Handle comments, ignore until newline */
|
||||||
|
else if is_comment_start(index)
|
||||||
|
then ( while ord(substring(str, index, 1)) <> ord("\n")
|
||||||
|
do index := index + 1
|
||||||
|
; ignore_ws())
|
||||||
|
|
||||||
|
|
||||||
function parse_rec (): sexp_ast =
|
function parse_rec (): sexp_ast =
|
||||||
( ignore_ws()
|
( ignore_ws()
|
||||||
|
@ -485,6 +498,8 @@ let /* Booleans */
|
||||||
var OPCODE_CAR := 20
|
var OPCODE_CAR := 20
|
||||||
var OPCODE_CDR := 16
|
var OPCODE_CDR := 16
|
||||||
var OPCODE_SET := 21
|
var OPCODE_SET := 21
|
||||||
|
var OPCODE_FRSTR := 22
|
||||||
|
var OPCODE_COMPILE:= 23
|
||||||
|
|
||||||
var vm_insn_num_opcodes := 0
|
var vm_insn_num_opcodes := 0
|
||||||
var vm_insn_info :=
|
var vm_insn_info :=
|
||||||
|
@ -526,14 +541,18 @@ let /* Booleans */
|
||||||
; code(OPCODE_DEFFUN, "DEFFUN", 1, 0, 0)
|
; code(OPCODE_DEFFUN, "DEFFUN", 1, 0, 0)
|
||||||
; code(OPCODE_POP, "POP", 0, 0, 0)
|
; code(OPCODE_POP, "POP", 0, 0, 0)
|
||||||
; code(OPCODE_GEQ, "GEQ", 0, 0, 0)
|
; code(OPCODE_GEQ, "GEQ", 0, 0, 0)
|
||||||
; code(OPCODE_OUTPUT, "OUTPUT", 0, 0, 0)
|
|
||||||
; code(OPCODE_TOSTR, "TOSTR", 0, 0, 0)
|
|
||||||
|
|
||||||
; code(OPCODE_CONS, "CONS", 0, 0, 0)
|
; code(OPCODE_CONS, "CONS", 0, 0, 0)
|
||||||
; code(OPCODE_CAR, "CAR", 0, 0, 0)
|
; code(OPCODE_CAR, "CAR", 0, 0, 0)
|
||||||
; code(OPCODE_CDR, "CDR", 0, 0, 0)
|
; code(OPCODE_CDR, "CDR", 0, 0, 0)
|
||||||
; code(OPCODE_SET, "SET", 0, 1, 0)
|
; code(OPCODE_SET, "SET", 0, 1, 0)
|
||||||
|
|
||||||
|
; code(OPCODE_OUTPUT, "OUTPUT", 0, 0, 0)
|
||||||
|
; code(OPCODE_TOSTR, "TOSTR", 0, 0, 0)
|
||||||
|
; code(OPCODE_FRSTR, "FRSTR", 0, 0, 0)
|
||||||
|
|
||||||
|
; code(OPCODE_COMPILE,"COMPILE", 0, 0, 0)
|
||||||
|
|
||||||
; for i := 1 to expected_number_opcodes - 1
|
; for i := 1 to expected_number_opcodes - 1
|
||||||
do if a[i] <> nil & a[i-1] = nil
|
do if a[i] <> nil & a[i-1] = nil
|
||||||
then print(concatm("Error: Opcode info array incorrectly initialized!\n Opcode "
|
then print(concatm("Error: Opcode info array incorrectly initialized!\n Opcode "
|
||||||
|
@ -569,7 +588,11 @@ let /* Booleans */
|
||||||
else if b = nil | b.first = nil
|
else if b = nil | b.first = nil
|
||||||
then ()
|
then ()
|
||||||
else if (a.first = nil & a.last <> nil) | (a.first <> nil & a.last = nil)
|
else if (a.first = nil & a.last <> nil) | (a.first <> nil & a.last = nil)
|
||||||
then print(concat("Error: Instruction list invariant not maintained! First is ", concat(if a.first = nil then "" else "not ", concat( "nil, second is ", concat(if a.last = nil then "" else "not ", "nil\n")))))
|
then print(concatm( "Error: Instruction list invariant not maintained! First is "
|
||||||
|
, if a.first = nil then "" else "not "
|
||||||
|
, "nil, second is "
|
||||||
|
, if a.last = nil then "" else "not "
|
||||||
|
, "nil\n"))
|
||||||
else if a.first = nil
|
else if a.first = nil
|
||||||
then ( a.first := b.first
|
then ( a.first := b.first
|
||||||
; a.last := b.last )
|
; a.last := b.last )
|
||||||
|
@ -1354,9 +1377,15 @@ let /* Booleans */
|
||||||
|
|
||||||
let var tape_size := tape.length
|
let var tape_size := tape.length
|
||||||
var tape := tape.tape
|
var tape := tape.tape
|
||||||
|
var continue := true
|
||||||
|
|
||||||
function vm_update () =
|
function vm_update () =
|
||||||
if tape[ip] = nil
|
if not(continue)
|
||||||
then ip := -1
|
then ()
|
||||||
|
else if tape[ip] = nil
|
||||||
|
then run_error("Missing instruction in tape")
|
||||||
|
else if not (0 <= ip & ip < tape_size)
|
||||||
|
then run_error("Instruction pointer out of bounds")
|
||||||
|
|
||||||
/* Integer binary operators */
|
/* Integer binary operators */
|
||||||
else if let var op := tape[ip].opcode
|
else if let var op := tape[ip].opcode
|
||||||
|
@ -1544,10 +1573,10 @@ let /* Booleans */
|
||||||
; print("\n Scheme: ")
|
; print("\n Scheme: ")
|
||||||
; print(safe_substring(source, tape[ip].pos_l, tape[ip].pos_r))
|
; print(safe_substring(source, tape[ip].pos_l, tape[ip].pos_r))
|
||||||
; print("\n")
|
; print("\n")
|
||||||
; ip := -1
|
; continue := false
|
||||||
end
|
end
|
||||||
|
|
||||||
in while 0 <= ip & ip < tape_size
|
in while continue
|
||||||
do ( vm_update()
|
do ( vm_update()
|
||||||
; if DEBUG_PRINT_STACK
|
; if DEBUG_PRINT_STACK
|
||||||
then ( print("[")
|
then ( print("[")
|
||||||
|
@ -1555,42 +1584,20 @@ let /* Booleans */
|
||||||
; print("]: ")
|
; print("]: ")
|
||||||
; print(stack_to_string(stack))
|
; print(stack_to_string(stack))
|
||||||
; print("\n") ))
|
; print("\n") ))
|
||||||
; if not (0 <= ip & ip < tape_size)
|
|
||||||
then print("Died due to out of bounds instruction pointer.\n")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
/* Do stuff */
|
/* Do stuff */
|
||||||
|
|
||||||
var test_text := "(begin (define add-three (lambda (x y z) (+ (+ x z) y))) \
|
var test_text :=
|
||||||
\ (define fac (lambda (x) (if (>= x 1) (* x (fac (+ x -1))) 1))) \
|
/* TODO: Improve top-level parsing */
|
||||||
\ (define (faca x a) \
|
let var text := "(begin "
|
||||||
\ (if (>= x 1) \
|
var char := "BAD SHIT HAPPENED"
|
||||||
\ (faca (+ x -1) (* x a)) \
|
in while char <> ""
|
||||||
\ a)) \
|
do ( char := getchar()
|
||||||
\ (define (f . l) (cdr l)) \
|
; text := concat(text, char) )
|
||||||
\ (define x 5) \
|
; concat(text, " )")
|
||||||
\ (define y '(1 2 3)) \
|
end
|
||||||
\ (display x) (newline) \
|
|
||||||
\ (display (fac x)) (newline) \
|
|
||||||
\ (display (faca x 1)) (newline) \
|
|
||||||
\ (display y) (newline) \
|
|
||||||
\ (display (car y)) (newline) \
|
|
||||||
\ (display (cdr y)) (newline) \
|
|
||||||
\ (set! x 10) \
|
|
||||||
\ (display x) (newline) \
|
|
||||||
|
|
||||||
\ (define-syntax and \
|
|
||||||
\ (syntax-rules ()\
|
|
||||||
\ ((and) #t)\
|
|
||||||
\ ((and test) test)\
|
|
||||||
\ ((and test1 test2 ...)\
|
|
||||||
\ (if test1 (and test2 ...) #f))))\
|
|
||||||
|
|
||||||
\ (display (and #t #t)) (newline) \
|
|
||||||
\ (display (and #f #t)) (newline) \
|
|
||||||
\ (display (and #f #f)) (newline) \
|
|
||||||
\ (display (and #t #f)) (newline) \
|
|
||||||
\) "
|
|
||||||
var ignore := ( print("** Parsing **\n")
|
var ignore := ( print("** Parsing **\n")
|
||||||
; print("Original : ")
|
; print("Original : ")
|
||||||
; print(test_text)
|
; print(test_text)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user