Permalink
Browse files

Merge branch 'master' of github.com:mwatts15/Crono

Conflicts:
	crono/prelude.lisp

* changed <COND> to my_cond
  • Loading branch information...
2 parents 7eefb1c + 202adb4 commit 9bb2aed7a4c8b80a13d00b770e15c6035575d8ba Mark Watts committed Nov 29, 2012
View
@@ -1,3 +1,25 @@
+%%%%
+% Base Predicates
+%%%%
+(define nil? (= Nil))
+(define zero? (= 0))
+(define t? (= #t))
+
+(defun any? (value) (= (typeof value) :any))
+(defun array? (value) (= (typeof value) :array))
+(defun atom? (value) (= (typeof value) :atom))
+(defun char? (value) (= (typeof value) :char))
+(defun cons? (value) (= (typeof value) :cons))
+(defun float? (value) (= (typeof value) :float))
+(defun func? (value) (= (typeof value) :func))
+(defun int? (value) (= (typeof value) :int))
+(defun number? (value) (= (typeof value) :number))
+(defun primitive? (value) (= (typeof value) :primitive))
+(defun string? (value) (= (typeof value) :string))
+(defun struct? (value) (= (typeof value) :struct))
+(defun type? (value) (= (typeof value) :type))
+(defun vector? (value) (= (typeof value) :vector))
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% boolean functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -7,6 +29,11 @@
(define or (\ (a b) (if a #t b)))
+%%%%
+% Extra predicates
+%%%%
+(defun boolean? (value) (or (nil? value) (t? value)))
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% higher order functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -27,37 +54,43 @@
(defun mod (x y) (if (< x y) x (mod (- x y) y)))
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% combinators
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+(define pred (+ -1))
+(define succ (+ 1))
+
%identity combinator
(define <I> (\ (x) x))
%Constant combinator
(define <K> (\ (x y) x))
%General application combinator
-(define <S> (\ (f g x) (f x (g x))))
+(define <S> (\ (f g p) ((f p) (g p))))
%Function composition combinator
(define <B> (\ (f g x) (f (g x))))
+% (define <B> (<S> (<K> <S>) <K>))
%
-(define <C> (\ (f x y) (f y x)))
-
-%(define <Y> (f)
-% (f (<Y> f)) )
-%Cannata's incorrect definition of Y combinator
+(define <C> (\ (f x y) ((f y) x)))
+% (define <C> (<S> (<S> (<K> (<S> (<K> <S>) <K>)) <S>) (<K> <K>)))
%Recursive function
-(define <Z> (\ (f)
- ((\ (x) (f (\ (v) ((x x) v))))
- (\ (x) (f (\ (v) ((x x) v)))))))
-
(define <Y> (\ (f)
- (\ (x) (f (x x))) (\ (x) (f (x x))) ))
-
-(define <COND> (\ (p f g x) (if (p x) (f x) (g x))))
-
-(define prFoldr (\ (fn z x)
- (<Y> (<B> (<COND> (= nil) (<K> z)) (<B> (<S> (<B> fn (car)) ) (<C> <B> cdr))) x)))
+ ((\ (x) (f (\ (v) ((x x) v))))
+ (\ (y) (f (\ (w) ((y y) w)))))))
+%(define <Y> (\ (f)
+% (\ (x) (f (x x))) (\ (x) (f (x x))) ))
+
+(define my_cond (\ (p f g x) (if (p x) (f x) (g x))))
+
+% (define prFoldr (\ (fn z x)
+% (<Y> (<B> (my_cond (= nil) (<K> z)) (<B> (<S> (<B> fn (car)) ) (<C> <B> cdr))) x)))
+(defun pradd1 (x z)
+ (<Y> (<B>
+ (my_cond (= 0) (<K> z))
+ (<B> (<S> (<B> + (<K> 1))) (<C> <B> pred)))
+ x))
View
@@ -18,6 +18,7 @@
new Option('e', "show-environment"),
new Option('h', "help"),
new Option('p', "print-ast"),
+ new Option('P', "no-prelude"),
new Option('q', "quiet"),
new Option('s', "static"),
new Option('t', "show-types"),
@@ -28,54 +29,55 @@
public static final String introstr =
"Crono++ by Mark Watts, Carlo Vidal, Troy Varney (c) 2012\n";
public static final String prompt = "> ";
+ public static final String prelude = "./prelude.lisp";
+ public static boolean showTypes = false;
+ public static boolean interactive = false;
+ public static boolean loadPrelude = true;
+ public static Visitor v = null;
+ public static List<String> files = new LinkedList<String>();
private static CronoType getStatement(Parser p) {
- /* This was supposed to loop the parser until it got a valid statement
- * or hit EOF, but I can't get it to work quite right */
- CronoType statement = null;
System.out.print(prompt);
try {
- statement = p.statement();
+ CronoType statement = p.statement();
+ if(!interactive) {
+ System.out.println((statement == null) ? "" : statement);
+ }
+ return statement;
}catch(ParseException pe) {
System.err.println(pe);
- statement = null;
}
-
- return statement;
+ return null;
}
- public static void main(String[] args) {
- boolean showTypes = false;
+ public static boolean parseOptions(Interpreter interp, String[] args) {
OptionParser optparse = new OptionParser(args);
- Interpreter interp = new Interpreter();
- Visitor v = interp;
- boolean interactive = (System.console() != null); /*< Java 6 feature */
- List<String> files = new LinkedList<String>();
-
int opt = optparse.getopt(options);
while(opt != -1) {
switch(opt) {
case 'd':
interp.dynamic(true);
break;
case 'D':
- interp.showEnv(true);
- interp.printAST(true);
- interp.trace(true);
+ interp.debug(true);
break;
case 'e':
interp.showEnv(true);
break;
case 'h':
System.err.println(helpstr);
- return;
+ return false;
case 'p':
interp.printAST(true);
break;
+ case 'P':
+ loadPrelude = false;
+ break;
case 'q':
interp.showEnv(false);
interp.printAST(false);
interp.trace(false);
+ interp.debug(false);
break;
case 's':
interp.dynamic(false);
@@ -92,72 +94,96 @@ public static void main(String[] args) {
System.err.printf("Invalid option: %s\n",
optparse.optchar);
System.err.println(helpstr);
- return;
+ return false;
}
opt = optparse.getopt(options);
}
for(int i = optparse.optind(); i < args.length; ++i) {
files.add(args[i]);
}
+ return true;
+ }
+
+ public static void main(String[] args) {
+ Interpreter interp = new Interpreter();
+ v = interp;
+ interactive = (System.console() != null); /*< Java 6 feature */
+
+ parseOptions(interp, args);
- Parser parser = null;
try {
File package_dir = new File("./packages/");
CronoPackage.initLoader(new URL[]{package_dir.toURI().toURL()});
}catch(MalformedURLException murle) {
System.err.printf("Crono: Could not open package directory!\n");
}
- if(interactive && files.size() == 0) {
- parser = new Parser(new InputStreamReader(System.in));
- System.out.println(introstr);
-
- boolean good = false;
- CronoType statement = getStatement(parser);
- while(statement != null) {
- try{
- statement = statement.accept(v);
- if(showTypes) {
- System.out.printf("Result: %s [%s]\n",statement.repr(),
- statement.typeId());
- }else {
- System.out.printf("Result: %s\n", statement.repr());
- }
- }catch(InterpreterException re) {
- String message = re.getMessage();
- if(message != null) {
- System.err.println(message);
- }else {
- System.err.println("Unknown Interpreter Error!");
- }
- }catch(RuntimeException re) {
- re.printStackTrace();
- }
- statement = getStatement(parser);
- }
-
- System.out.println();
+ if(files.size() == 0) {
+ interactive();
}else {
for(String fname : files) {
- try {
- parser = new Parser(new FileReader(fname));
- }catch(FileNotFoundException fnfe) {
- System.err.printf("Could not find %s:\n %s\n", fname,
- fnfe.toString());
- continue;
+ loadPrelude();
+ parseFile(fname);
+ v.reset(); /*< Reset the visitor for the next file */
+ }
+ }
+ }
+
+ public static void loadPrelude() {
+ if(loadPrelude) {
+ parseFile(prelude);
+ }
+ }
+
+ public static void interactive() {
+ System.out.println(introstr);
+ loadPrelude();
+
+ Parser parser = new Parser(new InputStreamReader(System.in));
+ CronoType statement = getStatement(parser);
+ while(statement != null) {
+ try{
+ statement = statement.accept(v);
+ if(showTypes) {
+ System.out.printf("Result: %s [%s]\n",statement.repr(),
+ statement.typeId());
+ }else {
+ System.out.printf("Result: %s\n", statement.repr());
}
-
- try {
- CronoType head = parser.program();
- head.accept(v);
- } catch(ParseException pe) {
- System.err.printf("Error parsing crono file: %s\n %s\n",
- fname, pe);
+ }catch(InterpreterException re) {
+ String message = re.getMessage();
+ if(message != null) {
+ System.err.println(message);
+ }else {
+ System.err.println("Unknown Interpreter Error!");
}
-
- v.reset(); /*< Reset the visitor for the next file */
+ }catch(RuntimeException re) {
+ re.printStackTrace();
}
+ statement = getStatement(parser);
+ }
+
+ System.out.println();
+ }
+
+
+ public static void parseFile(String fname) {
+ Parser parser = null;
+ try {
+ parser = new Parser(new FileReader(fname));
+ }catch(FileNotFoundException fnfe) {
+ System.err.printf("Could not find file %s:\n %s\n", fname, fnfe);
+ return;
+ }
+ try {
+ CronoType[] prog = parser.program();
+ for(int i = 0; i < prog.length; ++i) {
+ prog[i].accept(v);
+ }
+ }catch(ParseException pe) {
+ System.err.printf("Error parsing crono file: %s\n %s\n",fname,pe);
+ return;
}
}
}
Oops, something went wrong.

0 comments on commit 9bb2aed

Please sign in to comment.