# Environment Setup and Utilities
#------------------------------------------------------------------------------
+import platform
import fnmatch
import os
-import platform
# Helper function for recursively finding files
-def find_files(dir,pattern):
+def find_files(path,pattern):
matches = []
- for root, dirnames, filenames in os.walk(dir):
- for filename in fnmatch.filter(filenames, pattern):
+ for root, dirs, files in os.walk(path):
+ for filename in fnmatch.filter(files, pattern):
matches.append(os.path.join(root, filename))
return matches
# Scheme Source Compiler
scheme_compiler = Builder(
- action = 'csc -c -o $TARGET $SOURCE',
- suffix = '.o',
- src_suffix = '.scm',
+ action = 'csc $CCFLAGS -c -o $TARGET $SOURCE',
+ suffix = '.o',
+ src_suffix = '.scm',
single_source = True
)
# Scheme Binary Linker
scheme_linker = Builder(
- action = 'csc -o $TARGET $SOURCE',
- suffix = "$PROGSUFFIX",
+ action = 'csc $LDFLAGS -o $TARGET $SOURCES',
+ suffix = "$PROGSUFFIX",
+ src_suffix = '.o',
src_builder = [ scheme_compiler ]
)
# Create the Environment for this project
env = Environment(
- ENV = os.environ,
+ ENV = os.environ,
+ CCFLAGS = [ '-explicit-use' ],
+ LDFLAGS = [],
BUILDERS = { 'SchemeProgram': scheme_linker }
)
-(use srfi-1)
+(declare (unit core-forms) (uses srfi-1))
; Formal Syntax
;------------------------------------------------------------------------------
;
; Form ::= Definition | Expression
;
-; Definition ::= '(:def' Variable Expression ')'
-; | '(:begin' Definition* ')'
+; Definition ::= '(def' Variable Expression ')'
+; | '(begin' Definition* ')'
;
; Expression ::= Constant
; | Variable
-; | '(:quote' Datum ')'
-; | '(:func' ArgList Expression Expression* ')'
-; | '(:if' Expression Expression Expression ')'
-; | '(:set' Variable Expression ')'
-; | '(:apply' Expression Expression* ')'
+; | '(quote' Datum ')'
+; | '(func' ArgList Expression Expression* ')'
+; | '(if' Expression Expression Expression ')'
+; | '(set!' Variable Expression ')'
+; | '(' Expression Expression* ')'
;
; Constant ::= Boolean
; | Number
;
(define (validate-form frm)
+ (if (definition? frm)
+ (validate-definition frm)
+ (validate-expression frm)))
+
+(define (validate-definition frm)
+ (if (and (list? frm) (not (null? frm)))
+ (case (car frm)
+ [(begin) (map validate-definition (cdr frm))]
+ [(def) (if (not (def? frm))
+ (syntx-err frm "Not a valid def expression"))]
+ [else (syntx-err frm "Not a valid definition")])
+ (syntx-err frm "Not a valid definition")))
+
+(define (validate-expression frm)
(if (and (list? frm) (not (null? frm)))
(case (car frm)
[(quote) (validate-quotation frm)]
[(func) (validate-func frm)]
[(if) (validate-if frm)]
- [(set) (validate-set frm)]
+ [(set!) (validate-set frm)]
[else (validate-apply frm)])
(validate-constant frm)))
(define (set? frm)
(and (list-of-length? frm 3)
- (equal? 'set (car frm))
+ (equal? 'set! (car frm))
(symbol? (cadr frm))
(expression? (caddr frm))))
(define (get-free-vars expr)
(if (symbol? expr) expr
(case (if (list? expr) (car expr) '())
- [(def set) (list (cadr expr) (get-free-vars (caddr expr)))]
+ [(def set!) (list (cadr expr) (get-free-vars (caddr expr)))]
[(begin if) (map get-free-vars (cdr expr))]
[(func) (filter-vars (cadr expr) (map get-free-vars (cddr expr)))]
[else (map get-free-vars expr)])))
; Utility Procedures
;------------------------------------------------------------------------------
(define (syntx-err frm msg)
+ (print "Error: " msg "\n")
(pretty-print frm)
- (error msg))
-
-; Compilation Procedures
-;------------------------------------------------------------------------------
-(define (compile-file file) '())
-
-(define (compile-port port) '())
-
-(define (translate-expr expr) '())
-
-(print "foo")
+ (display "\n")
+ (exit 1))
--- /dev/null
+(declare (uses library eval core-forms))
+
+(define (start-repl prt)
+ (display ":> ")
+ (let [(form (read prt))]
+ (validate-form form)
+ (print (eval (core-form->scheme form)
+ (interaction-environment))))
+ (start-repl prt))
+
+(define (core-form->scheme frm)
+ (if (and (list? frm) (not (null? frm)))
+ (case (car frm)
+ [(def) (cons 'define (map core-form->scheme (cdr frm)))]
+ [(func) (cons 'lambda (map core-form->scheme (cdr frm)))]
+ [else (map core-form->scheme frm)])
+ frm))
+
+(start-repl (current-input-port))
+