Hi,

The attached patch provides much better support for the jolt-burg
grammar features that aren't implemented in
function/jolt2/ColaGrammar.st.  Please reverse my last
jolt2-fixes.patch (if you applied it) before applying this one.

Inspired by OMeta, I added a new 'EXTENDS:' keyword (which can appear
after the grammar rules but before the optional start expression)
which allows you to import rules from another grammar.  The import
doesn't overwrite existing rules, and you can use EXTENDS: as many
times as you like.

With that established, I added a new ColaGrammar-derived grammar to
jolt2/boot.k that implements the missing features.  Reading that file
in conjunction with function/jolt2/ColaGrammar.st and the other
function/examples2 parsers should give you a good idea of how to use
jolt2's parsing features.  It should be possible to strip even more
out of ColaGrammar and put it into boot.k.  I'll leave that as an
exercise for the reader.

Maybe now would be a good time to update the Brainf*ck tutorial?

[Note that function/examples/peg still segfaults.  I haven't
investigated it yet, and I probably won't unless I'm really bored.]

Have fun,

-- 
Michael FIG <[EMAIL PROTECTED]> //\
   http://michael.fig.org/    \//

Get jolt2 and examples2 to work.

diff -r 2c52027901c7 function/examples/cairo/Makefile
--- a/function/examples/cairo/Makefile	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/examples/cairo/Makefile	Wed Apr 16 23:27:46 2008 -0600
@@ -6,7 +6,7 @@ run test : cairolib.so .FORCE
 	$(JOLT) boot.k main.k
 
 cairolib.so : cairolib.st
-	$(IDC) $(CAIROINC) -I../../objects -s $<
+	$(IDC) -g -k $(CAIROINC) -I../../objects -s $<
 
 clean : .FORCE
 	rm -f *~ *.so *.o
diff -r 2c52027901c7 function/examples/cairo/libcairo.k
--- a/function/examples/cairo/libcairo.k	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/examples/cairo/libcairo.k	Wed Apr 16 23:27:47 2008 -0600
@@ -1,5 +1,4 @@
 (dlopen "libcairo")
-(define cairolib (dlopen "cairolib"))
-((_dlsym cairolib "__id__init__"))
+[Object _import: "cairolib"]
 
 (define libcairo	(import "libcairo"))
diff -r 2c52027901c7 function/examples/regexp/Makefile
--- a/function/examples/regexp/Makefile	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/examples/regexp/Makefile	Wed Apr 16 23:27:47 2008 -0600
@@ -12,55 +12,55 @@ test1 : .FORCE
 test1 : .FORCE
 	time egrep   'break|continue|for|while|switch|case|return|static|extern' < tests/test.c | tee out.e | wc -l
 	time $(GREP) 'break|continue|for|while|switch|case|return|static|extern' < tests/test.c | tee out.p | wc -l
-	diff out.e out.p
+	cmp out.e out.p
 	rm out.e out.p
 
 test2 : .FORCE
 	time egrep   'd?e?l?t?a?' < tests/termcap | tee out.e | wc -l
 	time $(GREP) 'd?e?l?t?a?' < tests/termcap | tee out.p | wc -l
-	diff out.e out.p
+	cmp out.e out.p
 	rm out.e out.p
 
 test3 : .FORCE
 	time egrep   'd?e+l*t?a+' < tests/termcap | tee out.e | wc -l
 	time $(GREP) 'd?e+l*t?a+' < tests/termcap | tee out.p | wc -l
-	diff out.e out.p
+	cmp out.e out.p
 	rm out.e out.p
 
 test4 : .FORCE
 	time egrep   'd?e+l*t?a+' < tests/termcap | tee out.e | wc -l
 	time $(GREP) 'd?e+l*t?a+' < tests/termcap | tee out.p | wc -l
-	diff out.e out.p
+	cmp out.e out.p
 	rm out.e out.p
 
 test5 : .FORCE
 	time egrep   'p[aeiou]t'  < tests/termcap | tee out.e | wc -l
 	time $(GREP) 'p[aeiou]t'  < tests/termcap | tee out.p | wc -l
-	diff out.e out.p
+	cmp out.e out.p
 	rm out.e out.p
 
 test6 : .FORCE
 	time egrep   'p[^aeiou]t' < tests/termcap | tee out.e | wc -l
 	time $(GREP) 'p[^aeiou]t' < tests/termcap | tee out.p | wc -l
-	diff out.e out.p
+	cmp out.e out.p
 	rm out.e out.p
 
 test7 : .FORCE
 	time egrep   '19[0-9]+'   < tests/termcap | tee out.e | wc -l
 	time $(GREP) '19[0-9]+'   < tests/termcap | tee out.p | wc -l
-	diff out.e out.p
+	cmp out.e out.p
 	rm out.e out.p
 
 test8 : .FORCE
 	time egrep   '19[^0-9]+'  < tests/termcap | tee out.e | wc -l
 	time $(GREP) '19[^0-9]+'  < tests/termcap | tee out.p | wc -l
-	diff out.e out.p
+	cmp out.e out.p
 	rm out.e out.p
 
 test9 : .FORCE
 	time egrep   '..a.....b'  < tests/termcap | tee out.e | wc -l
 	time $(GREP) '..a.....b'  < tests/termcap | tee out.p | wc -l
-	diff out.e out.p
+	cmp out.e out.p
 	rm out.e out.p
 
 
diff -r 2c52027901c7 function/jolt2/ColaGrammar.st
--- a/function/jolt2/ColaGrammar.st	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/jolt2/ColaGrammar.st	Wed Apr 16 23:27:47 2008 -0600
@@ -17,6 +17,7 @@ ColaParsingGrammar	:= [ Grammar new ]
 	at: #comment	put: ( '//' parse , (#eol parse not , PeAny) zeroMore , #eol parse);
 	at: #_		put: ( _ := ( (#space parse | #comment parse) zeroMore ) ).
     g	at: #pling	put: '!'  parse , _;
+    	at: #EXTENDS	put: 'EXTENDS:' parse , _;
     	at: #lparen	put: '('  parse , _;
     	at: #rparen	put: ')'  parse , _;
     	at: #query	put: '?'  parse , _;
@@ -67,9 +68,11 @@ ColaParsingGrammar	:= [ Grammar new ]
 						     | ( ( #larrow parse , #number   parse <- [:p | ([EMAIL PROTECTED]) <- p result] ->0 ) zeroOne  <- [:p | [EMAIL PROTECTED] ) ) ).
     g	at: #sequence	put: ( #suffix   parse ->0 , (                 #suffix   parse <- [:p | ([EMAIL PROTECTED]) ,  p result] ->0 ) zeroMore <- [:p | [EMAIL PROTECTED] ).
     g	at: #expression	put: ( #sequence parse ->0 , ( #bar parse    , #sequence parse <- [:p | ([EMAIL PROTECTED]) |  p result] ->0 ) zeroMore <- [:p | [EMAIL PROTECTED] ).
-    g	at: #grammar	put: ( [Grammar new] setResult ->0
+    g	at: #grammar	put: ( [Grammar new] setResult ->0 , _
 			     , ( #identifier parse ->1 , #equal parse , #expression parse , #semicolon parse zeroOne	<- [:p | [EMAIL PROTECTED] at: [EMAIL PROTECTED] put: p result]	) zeroMore
+			     , ( #EXTENDS parse , ( ColaFunctionGrammar-#expression ) <- [:p | [EMAIL PROTECTED] extends: p result _eval] , _		) zeroMore
 			     , ( #expression parse									<- [:p | [EMAIL PROTECTED] startRule: p result]	) zeroOne
+			     
 			     ) <- [:p | ([EMAIL PROTECTED]) finalise "println"].
 ]
 
@@ -90,7 +93,7 @@ ColaParsingGrammar	:= [ Grammar new ]
     g	at: #decimal	put: ( #digit parse oneMore textResult							<- [:p | p result inject: 0 into: [:n :c | n * 10 + c digitValue]] );	
 	at: #heximal	put: ( ( '0x' parse , ( #digit parse | #hex parse ) oneMore ) textResult		<- [:p | p result inject: 0 into: [:n :c | n * 16 + c digitValue]] );
 	at: #posfloat	put: ( #decimal parse -> 0 , '.' parse , ( #digit parse oneMore textResult ) -> 1	<- [:p | ([EMAIL PROTECTED] reverseInject: 0.0 into: [:n :c | n / 10.0 + c digitValue]) / 10.0 + ([EMAIL PROTECTED])] );
-	at: #posnumber	put: ( #decimal parse | #heximal parse | #posfloat parse );
+	at: #posnumber	put: ( #heximal parse | #posfloat parse | #decimal parse );
 	at: #number	put: ( ( '-' parse          , #posnumber parse						<- [:p | p result negated] )
 			     | ( '+' parse zeroMore , #posnumber parse ) ).
     g	at: #plainChar	put: ( PeAny textResult <- [:p | p last] );
@@ -109,8 +112,8 @@ ColaParsingGrammar	:= [ Grammar new ]
     g	at: #unary	put: ( ( #alpha parse , ( #alpha parse | #digit parse ) zeroMore ) textResult, _ );
 	at: #binary	put: ( ( '-+!%&*,/<=>[EMAIL PROTECTED]|~' parseClass oneMore ) textResult , _ );
 	at: #keyword	put: ( ( #alpha parse , ( #alpha parse | #digit parse ) zeroMore , ':' parse ) textResult , _ ).
-    g	at: #unymsg	put: ( #unary   parse ->0						<- [:p | Array with: [EMAIL PROTECTED] );
-	at: #binmsg	put: ( #binary  parse ->0 , #expr parse ->1 , _				<- [:p | Array with: [EMAIL PROTECTED] with: [EMAIL PROTECTED] );
+    g	at: #unymsg	put: ( #unary   parse ->0						<- [:p | OrderedCollection with: [EMAIL PROTECTED] );
+	at: #binmsg	put: ( #binary  parse ->0 , #expr parse ->1 , _				<- [:p | OrderedCollection with: [EMAIL PROTECTED] with: [EMAIL PROTECTED] );
 	at: #keyarg	put: ( #keyword parse ->0 , #expr parse ->1 , _				<- [:p | OrderedCollection with: [EMAIL PROTECTED] with: [EMAIL PROTECTED] );
 	at: #keymsg	put: ( #keyarg  parse ->0 , ( #keyword parse ->1 , #expr parse ->2 , _	<- [:p | ([EMAIL PROTECTED]) first: ([EMAIL PROTECTED]) first, ([EMAIL PROTECTED]); add: ([EMAIL PROTECTED]); yourself]
 							  ) zeroMore				<- [:p | ([EMAIL PROTECTED])]
@@ -129,7 +132,7 @@ ColaParsingGrammar	:= [ Grammar new ]
 	at: #expression	put: _ , ( #expr parse | #eof parse | #error parse ).
 ]
 
-"mutually recursive rules between the above grammars can now be resolve"
+"mutually recursive rules between the above grammars can now be resolved"
 
 [
     ColaParsingGrammar startSymbol: #grammar.
diff -r 2c52027901c7 function/jolt2/Compiler.st
--- a/function/jolt2/Compiler.st	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/jolt2/Compiler.st	Wed Apr 16 23:27:47 2008 -0600
@@ -919,7 +919,7 @@ Compiler xEvaluate: form
 
 Compiler errorUndefined: aSymbol	[ self error: 'undefined: ', aSymbol ]
 Compiler errorSyntax: form		[ self error: 'syntax error: ', form printString ]
-Compiler errorArgumentCount: form	[ self error: 'wrong number of aguments: ', form printString ]
+Compiler errorArgumentCount: form	[ self error: 'wrong number of arguments: ', form printString ]
 Compiler errorLoop: form		[ self error: 'no loop: ', form printString ]
 
 "----------------------------------------------------------------"
diff -r 2c52027901c7 function/jolt2/Grammar.st
--- a/function/jolt2/Grammar.st	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/jolt2/Grammar.st	Wed Apr 16 23:27:47 2008 -0600
@@ -39,6 +39,11 @@ Grammar startRule: pe
     startRule := PeRule withValue: pe name: #'<start>'.
     dirty := true.
     self finalise.
+]
+
+Grammar extends: aGrammar
+[
+    aGrammar do: [:assoc | self at: assoc key ifAbsentPut: assoc value].
 ]
 
 Grammar finalise
diff -r 2c52027901c7 function/jolt2/Makefile
--- a/function/jolt2/Makefile	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/jolt2/Makefile	Wed Apr 16 23:27:47 2008 -0600
@@ -28,7 +28,10 @@ all : main$(OBJEXT)
 all : main$(OBJEXT)
 
 test : main$(OBJEXT) .FORCE
-	./main$(OBJEXT) boot.k sanaras.k
+	./main$(OBJEXT) -L../jolt-burg boot.k sanaras.k
+
+test-old: main$(OBJEXT) .FORCE
+	cd ../examples && $(MAKE) test JOLT="../../jolt2/main$(OBJEXT) -L../../jolt-burg"
 
 .SUFFIXES :
 
diff -r 2c52027901c7 function/jolt2/Options.st
--- a/function/jolt2/Options.st	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/jolt2/Options.st	Wed Apr 16 23:27:47 2008 -0600
@@ -20,13 +20,15 @@
 
 { import: Objects }
 
-Options : Object ( verbose compile list libdir )
+Options : Object ( verbose compile list libdir progdir )
 
 Options verbose		[ ^verbose ]
 Options compile		[ ^compile ]
 Options list		[ ^list ]
 Options libdir: aString	[  libdir := aString ]
 Options libdir		[ ^libdir ]
+Options progdir: aDir	[  progdir := aDir ]
+Options progdir		[ ^progdir ]
 
 Options new
 [
diff -r 2c52027901c7 function/jolt2/Parser.st
--- a/function/jolt2/Parser.st	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/jolt2/Parser.st	Wed Apr 16 23:27:47 2008 -0600
@@ -113,5 +113,5 @@ Parser errorContents
 
 Parser setResultFromFunction_: _function
 {
-    self->v_result= ((oop(*)())v__function)(v__closure, self, self, self->v_result);
+    self->v_result= ((oop(*)())v__function)(v__closure, v_stateful_self, v_self, self->v_result);
 }
diff -r 2c52027901c7 function/jolt2/boot.k
--- a/function/jolt2/boot.k	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/jolt2/boot.k	Wed Apr 16 23:27:47 2008 -0600
@@ -191,16 +191,50 @@
 		    | error )
       } match: node]))
 
-;; methods
-
-(define add-method	; type selector implementation
-  (lambda (type selector implementation)
-    [[type _vtable] methodAt: selector put: implementation with: 0]))
-
-(syntax define-send ; selector type args... expr
-  (lambda (form compiler)
-    (let ((selector [form second])
-	  (type     [form third])
-	  (args     [form copyFrom: '3 to: [[form size] - '2]])
-	  (expr     [form last]))
-      `(add-method ,type ,selector (lambda (_closure _self self ,@args) ,expr)))))
+;; Add features that aren't present in the original grammar.
+(define ColaFunctionGrammar (import "ColaFunctionGrammar"))
+(define Integer (import "Integer"))
+['{
+     // Add character literals ($CHAR)
+     atom = number | charLiteral | identifier | string
+     charLiteral = '$' char->0				<- [self @ '0]
+
+     // Add octal character escapes ("\\([0-3]?[0-7])?[0-7]").
+     escapeSeq = '\\' (escapeChar | octalChar | plainChar)
+     oct = [0-7]
+     octalChar = (([0-3] oct oct) | (oct oct) | oct) $	<- [Integer fromString: [self result] base: '8]
+
+     // Binary integers.
+     posnumber = heximal | binimal | posfloat | decimal
+     binimal = '0b' [0-1]+ $				<- [Integer fromString: [self result] base: '2]
+
+     // Variadic message sends ([obj msg expr...]).
+     send = '[' _ expr->0 _ message->1 _ (expr->2 _	<- [[self @ '1] add: [self @ '2]])*
+	    ']'	<- [Expression	with: 'send
+				with: [Expression with: 'quote with: [[[self @ '1] first] asSymbol]]
+				with: [self @ '0]
+				withAll: [self @ '1] from: '1]
+     ;
+     EXTENDS: ColaFunctionGrammar // Import from the default grammar.
+     expression // Start term
+  } name: 'JoltBurgCompatibleFunctionGrammar]
+
+(load "syntax.k")
+(load "debug.k")
+(load "object.k")
+
+(define *package-list* [IdentitySet new])
+
+(define %require
+  (lambda (package)
+    (or [*package-list* includes: [package asSymbol]]
+	(let ()
+	  (load [[[package asString] , '".k"] _stringValue])
+	  [*package-list* add: [package asSymbol]]))))
+
+(syntax require
+  (lambda (node comp)
+    (let ((requires [WriteStream on: [Expression new: '8]]))
+      (for (i '1 2 [[node size] - '1])
+	[requires nextPut: `(%require ,[node at: i])])
+      `(let () ,@[requires contents]))))
diff -r 2c52027901c7 function/jolt2/main.st
--- a/function/jolt2/main.st	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/jolt2/main.st	Wed Apr 16 23:27:47 2008 -0600
@@ -91,7 +91,10 @@ ColaEvaluator map: aBlock
 
 ColaEvaluator readFileNamed: fileName
 [
-    self readFile: (fileName = '-' ifTrue: [StdIn] ifFalse: [File open: fileName])
+    self readFile: (fileName = '-' ifTrue: [StdIn]
+    	 	   	     ifFalse: [((File openIfPresent: fileName)
+			     	        ifNil: [File openIfPresent: Options progdir, '/', fileName])
+					ifNil: [File open: Options libdir, '/', fileName]])
 ]
 
 ColaEvaluator readFile: aFile
@@ -108,9 +111,14 @@ ColaEvaluator readFile: aFile
 
 
 [
-    | done |
+    | done args arg |
     "ColaFunctionGrammar println."
+    Options progdir: (OS argumentAt: 0) dirname.
     Options libdir: '.'.
-    OS arguments do: [:arg | (Options parseOption: arg) ifFalse: [ColaEvaluator readFileNamed: (done := arg)]].
+    args := OS arguments.
+    [args notEmpty]
+        whileTrue:
+           [ arg := args removeFirst.
+	     (Options parseOption: arg) ifFalse: [ColaEvaluator readFileNamed: (done := arg)]].
     done ifFalse: [ColaEvaluator readFile: StdIn].
 ]
diff -r 2c52027901c7 function/objects/SequenceableCollection.st
--- a/function/objects/SequenceableCollection.st	Tue Apr 15 22:08:09 2008 -0600
+++ b/function/objects/SequenceableCollection.st	Wed Apr 16 23:27:47 2008 -0600
@@ -216,6 +216,12 @@ SequenceableCollection inject: result in
 SequenceableCollection inject: result into: binaryBlock
 [
     self do: [:element | result := binaryBlock value: result value: element].
+    ^result
+]
+
+SequenceableCollection reverseInject: result into: binaryBlock
+[
+    self reverseDo: [:element | result := binaryBlock value: result value: element].
     ^result
 ]
 
_______________________________________________
fonc mailing list
fonc@vpri.org
http://vpri.org/mailman/listinfo/fonc

Reply via email to