diff parser_old.rhope @ 49:3e20ed8959c4

Added initial FFI implementation, Array type and 64-bit integers
author Mike Pavone <pavone@retrodev.com>
date Thu, 08 Apr 2010 01:02:18 -0400
parents a24eb366195c
children 689fb73e7612
line wrap: on
line diff
--- a/parser_old.rhope	Tue Mar 02 00:18:49 2010 -0500
+++ b/parser_old.rhope	Thu Apr 08 01:02:18 2010 -0400
@@ -33,11 +33,12 @@
 	Uses
 	Hex Escape
 	Escape Map
+	Foreign
 }
 
 New@Parser[:out]
 {
-	out <- [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[Build["Parser"]
+	out <- [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[Build["Parser"]
 	]Arg Begin << ["["]
 	]Arg End <<["]"]
 	]Line Comment <<["//"]
@@ -68,6 +69,7 @@
 	]Hex Escape <<["x"]
 	]Uses <<["uses"]
 	]Escape Map <<[[[[Dictionary[]]Set["n","\n"]]Set["r","\r"]]Set["t","\t"]]
+	]Foreign <<["Foreign"]
 }
 
 Blueprint Output Reference
@@ -100,6 +102,19 @@
 	}
 }
 
+Blueprint Foreign Lib
+{
+	Language
+	Name
+}
+
+New Foreign Lib[language, library:out]
+{
+	out <- [[Build["Foreign Lib"]
+	]Language <<[language]
+	]Name <<[library]
+}
+
 Blueprint Parse Program
 {
 	Workers
@@ -645,7 +660,7 @@
 	out <- [[[Build["Parse Error"]]Type <<[type]]Text <<[text]]Line Number <<[number]
 }
 
-Filter Empty[string:out]
+Not Empty[string:out]
 {
 	If[[[string]Length] > [0]]
 	{
@@ -804,7 +819,7 @@
 		{
 			body lines <- [body]Split["\n"]
 			more lines <- [[[body lines]Length] - [1]] + [name lines]
-			fields <- Fold[["Process Blueprint Field"]Set Input[2, params], (), Filter[Map[body lines, ["Trim"]Set Input[1,"\n\r\t "]], "Filter Empty"]]
+			fields <- Fold[["Process Blueprint Field"]Set Input[2, params], (), Filter[Map[body lines, ["Trim"]Set Input[1,"\n\r\t "]], "Not Empty"]]
 			new tree <- [tree]Blueprints << [ [[tree]Blueprints >>]Set[name, New@Blueprint Definition[name, fields]] ]
 			out <- Null[~, params, new tree, [lines] + [more lines]]
 		} {} {
@@ -1196,15 +1211,21 @@
 				{
 					,warn <- If[[Type Of[~]] = ["Named Pipe Node"]]
 					{
-						paramtype,warn <- [before variant]Set Variant[[variant]Name >>]
+						before mutable,warn <- [before variant]Set Variant[[variant]Name >>]
 					}
 					Val[warn]
 					{
 						Print[[[["Warning: Invalid variant for type "]Append[[before variant]Name >>]]Append[" on input "]]Append[param name]]
-						paramtype <- Val[before variant]
+						before mutable <- Val[before variant]
 					}
 				}{
-					paramtype <- Val[before variant]
+					before mutable <- Val[before variant]
+				}
+				[type info]Index[2]
+				{
+					paramtype <- [before mutable]Mutable? <<[ [[~]Name >>] = ["Mutable"] ]
+				}{
+					paramtype <- Val[before mutable]
 				}
 			}{
 				paramtype <- Type Instance["Any Type"]
@@ -1228,7 +1249,7 @@
 	}
 }
 
-Worker Name[string,params,tree,lines:out]
+Worker Declaration[string,params:worker,rest,no match]
 {
 	,whitespace name <- [string]Get Comment DString[[params]Arg Begin >>, params]
 	{
@@ -1245,40 +1266,79 @@
 			//if it isn't present, everything in the arglist is an input
 			If[[~] = [in out]]
 			{
-				after arglist <- [after]Get Comment DString[arg end, params] {}
+				rest <- [after]Get Comment DString[arg end, params] {}
 				{
 					outputs,output types <- Parse Param List[~, (), (), params]
 					{ Print["got outputs"] }
 					{ Print["got output types"] }
 				}
 			}{
-				after arglist <- Val[after]
+				rest <- Val[after]
 				outputs <- ()
 				output types <- ()
 			}
 			inputs,input types <- Parse Param List[arglist, (), (), params]
 			{ Print["got inputs"] }
 			{ Print["got input types"] }
-			
-			//inputs <- Map[[arglist]Split[[params]List Delim >>], ["Trim"]Set Input[1,"\n\r\t "]]
-			//outputs <- Map[[output string]Split[[params]List Delim >>], ["Trim"]Set Input[1,"\n\r\t "]]
-
-			New@Parse Worker[worker name, inputs, outputs, input types, output types, 0]
-			{
-				Print["Got parse worker"]
-				body text, modifiers <- [after arglist]Get Comment DString[[params]Block Begin >>, params]
-				modified <- Process Modifiers[~, params, modifiers]
-				expression trees, after body <- Worker Body[body text, params, ()]
-				worker <- [modified]Trees <<[expression trees]
-				new worker dict <- [[tree]Workers >>]Set[worker name, worker]
-				out <- Null[after body, params, [tree]Workers <<[new worker dict], 0]
-			}
+			worker <- New@Parse Worker[worker name, inputs, outputs, input types, output types, 0]
 		}
 	}{}{}{
+		no match <- string
+	}
+}
+
+Worker Name[string,params,tree,lines:out]
+{
+	,after arglist <- Worker Declaration[string, params]
+	{
+		Print["Got parse worker"]
+		worker name <- [~]Name >>
+		body text, modifiers <- [after arglist]Get Comment DString[[params]Block Begin >>, params]
+		modified <- Process Modifiers[~, params, modifiers]
+		expression trees, after body <- Worker Body[body text, params, ()]
+		worker <- [modified]Trees <<[expression trees]
+		new worker dict <- [[tree]Workers >>]Set[worker name, worker]
+		out <- Null[after body, params, [tree]Workers <<[new worker dict], 0]
+	} {} {
 		out <- tree
 	}
 }
 
+Parse Foreign Worker[tree, string, lib, params:out]
+{
+	,rest <- Worker Declaration[string, params]
+	{
+		foreign <- [~]Trees << [lib]
+		next <- [tree]Workers << [[[tree]Workers >>]Set[[foreign]Name >>, foreign]]
+		out <- Parse Foreign Worker[next, rest, lib, params]
+	} {} {
+		out <- tree
+	}
+}
+
+Parse Foreign[string,params,tree,lines:out]
+{	
+	Print["Foreign block"]
+	,after foreign <- [string]Slice[[[params]Foreign >>]Length]
+	Print[[after foreign]Slice[20]]
+	[after foreign]Get Comment DString[[params]Blueprint Type Delim >>, params]
+	{
+		[~]Get Comment DString[[params]Block Begin >>, params]
+		{
+			rest, body <- [~]Get Comment DString[[params]Block End >>, params]
+		}
+		{ lib <- Trim[~, "\r\n\t "] }
+	}
+	{ language <- Trim[~, "\r\n\t "] }
+	Print[[[["Language: "]Append[language]]Append[" Library: "]]Append[lib]]
+	{
+	Parse Foreign Worker[tree, body, New Foreign Lib[language, lib], params]
+	{ 
+		out <- Null[rest, params, ~, 0]
+	}
+	}
+}
+
 Null[string,params,tree,lines:out]
 {
 	trimmed <- Comment Left Trim[string, " \n\r\t", params]
@@ -1291,7 +1351,12 @@
 		{
 			out <- Parse Import[trimmed, params, tree, current line]
 		}{
-			out <- Worker Name[trimmed, params, tree, current line]
+			If[[trimmed]Starts With[ [params]Foreign >> ]]
+			{
+				out <- Parse Foreign[trimmed, params, tree, current line]
+			}{
+				out <- Worker Name[trimmed, params, tree, current line]
+			}
 		}
 	}
 }
@@ -1311,16 +1376,31 @@
 Register Workers Compile[prog, worker, name:out]
 {
 	Print[["Registering "]Append[name]]
-	out <- [prog]Register Worker[name, "rhope", [[worker]Inputs >>]Length, [[worker]Outputs >>]Length]
+	If[[ Type Of[[worker]Trees >>] ] = ["Foreign Lib"]]
+	{ convention <- Val[[[worker]Trees >>]Language >>] }
+	{ convention <- "rhope" }
+	out <- [prog]Register Worker[name, convention, [[worker]Inputs >>]Length, [[worker]Outputs >>]Length]
 }
 
 Add Workers Compile[prog, worker, name:out]
 {
 	Print[["Add Workers Compile: "]Append[name]]
 	{
-	trees, nworker, refs <- Add List to Worker[[worker]Trees >>, [NWorker["rhope"]]Uses[[worker]Uses Stores >>], prog, worker, Dictionary[]]
-	final nworker <- Fold[[["Add Wires Helper"]Set Input[3, worker]]Set Input[4, refs], nworker, trees]
-	out <- [prog]Bind Worker[name, final nworker]
+		If[[Type Of[[worker]Trees >>]] = ["Foreign Lib"]]
+		{
+			//TODO: Handle foreign func
+			final nworker <- [[[[[NWorker[[[worker]Trees >>]Language >>]
+			]Inputs <<[ [worker]Inputs >> ]
+			]Input Types <<[ [worker]Input Types >> ]
+			]Outputs <<[ [worker]Outputs >> ]
+			]Output Types <<[ [worker]Output Types >> ]
+			]Library <<[ [[worker]Trees >>]Name >> ]
+			
+		}{
+			trees, nworker, refs <- Add List to Worker[[worker]Trees >>, [NWorker["rhope"]]Uses[[worker]Uses Stores >>], prog, worker, Dictionary[]]
+			final nworker <- Fold[[["Add Wires Helper"]Set Input[3, worker]]Set Input[4, refs], nworker, trees]
+		}
+		out <- [prog]Bind Worker[name, final nworker]
 	}
 }