changeset 247:b76f683d076e

Finish moving ast object definitions to a separate ast module
author Michael Pavone <pavone@retrodev.com>
date Wed, 08 Jan 2014 19:27:19 -0800
parents 8c81afd6d2d3
children 96fdc5b37ceb
files modules/ast.tp modules/parser.tp
diffstat 2 files changed, 144 insertions(+), 107 deletions(-) [+]
line wrap: on
line diff
--- a/modules/ast.tp	Mon Jan 06 19:41:35 2014 -0800
+++ b/modules/ast.tp	Wed Jan 08 19:27:19 2014 -0800
@@ -1,13 +1,24 @@
 {
-	_binary <- 0
-	_string <- 1
-	_int    <- 2
+	_binary     <- 0
+	_string     <- 1
+	_int        <- 2
+	_symbol     <- 3
+	_call       <- 4
+	_object     <- 5
+	_sequence   <- 6
+	_assignment <- 7
+	_lambda     <- 8
 
 	#{
 		binary <- { _binary }
 		stringlit <- { _string }
 		intlit <- { _int }
-
+		sym <- { _symbol }
+		call <- { _call }
+		obj <- { _object }
+		sequence <- { _sequence }
+		assignment <- { _assignment }
+		lambda <- { _lambda }
 
 		binaryOp:withArgs <- :opname :_left _right {
 			#{
@@ -80,5 +91,123 @@
 				}
 			}
 		}
+
+		symbol <- :_name {
+			#{
+				nodeType <- { _symbol }
+				name <- _name
+				stringIndent <- :indent {
+					name
+				}
+				string <- {
+					stringIndent: ""
+				}
+			}
+		}
+
+		funcall:withArgs:hasReceiver? <- :_tocall :_args :_receiver? {
+			#{
+				tocall <- _tocall
+				args <- _args
+				hasReceiver? <- _receiver?
+				stringIndent <- :indent {
+					argparts <- []
+					if: (tocall nodeType) = _symbol {
+						argparts <- (tocall name) splitOn: ":"
+					} else: {
+						argparts <- [tocall stringIndent: indent]
+					}
+					curarg <- args
+					str <- ""
+					if: hasReceiver? {
+						str <- ((curarg value) stringIndent: indent) . " "
+						curarg <- curarg tail
+					}
+					foreach: argparts :idx part {
+						str <- str . part . ":"
+						if: (not: (curarg empty?)) {
+							str <- str . " " . ((curarg value) stringIndent: indent)
+							curarg <- curarg tail
+						}
+					}
+					while: { not: (curarg empty?) } do: {
+						str <- str . " " . ((curarg value) stringIndent: indent)
+						curarg <- curarg tail
+					}
+					str
+				}
+				string <- {
+					stringIndent: ""
+				}
+			}
+		}
+
+		object <- :_messages {
+			#{
+				nodeType <- { _object }
+				messages <- _messages
+				stringIndent <- :indent {
+					nextindent <- "\t" . indent
+					(messages fold: "#{" with: :acc el {
+						acc . "\n" . nextindent . (el stringIndent: nextindent)
+					}) . "\n" . indent . "}"
+				}
+				string <- {
+					stringIndent: ""
+				}
+			}
+		}
+
+		seqLit:array? <- :_els :_array? {
+			#{
+				nodeType <- { _sequence }
+				els <- _els
+				array? <- _array?
+				stringIndent <- :indent {
+					nextIndent <- "\t" . indent
+					(els fold: (if: array? {"#["} else: {"["}) with: :acc el {
+						acc . "\n" . nextIndent . (el stringIndent: nextIndent)
+					}) . "\n" . indent . "]"
+				}
+				string <- {
+					stringIndent: ""
+				}
+			}
+		}
+
+		assign:to <- :_expr :_sym {
+			#{
+				nodeType <- { _assignment }
+				assign <- _expr
+				to <- _sym
+				stringIndent <- :indent {
+					(to stringIndent: indent) . " <- " . (assign stringIndent: indent)
+				}
+				string <- {
+					stringIndent: ""
+				}
+			}
+		}
+
+		lambda:withArgs <- :_exprs :_args {
+			#{
+				nodeType <- { _lambda }
+				args <- _args
+				expressions <- _exprs
+				stringIndent <- :indent {
+					argStr <- args join: " "
+					if: (argStr length) > 0 {
+						argStr <- argStr . " "
+					}
+					nextIndent <- "\t" . indent
+					(expressions fold: argStr . "{" with: :acc el {
+						acc . "\n" . nextIndent . (el stringIndent: nextIndent)
+					}) . "\n" . indent . "}"
+				}
+				string <- {
+					stringIndent: ""
+				}
+			}
+		}
 	}
 }
--- a/modules/parser.tp	Mon Jan 06 19:41:35 2014 -0800
+++ b/modules/parser.tp	Wed Jan 08 19:27:19 2014 -0800
@@ -698,12 +698,7 @@
 	symexpr <- match: Name where: {
 		Name <- match: (onePlus: (charClass: "a-zA-Z_@!?")) . (zeroPlus: ((matchOne: [":" ""]) . (charClass: "a-zA-Z_@!?0-9")))
 	} yield: {
-		#{
-			name <- Name
-			string <- {
-				name
-			}
-		}
+		ast symbol: Name
 	}
 
 	namepart <- match: hws . Symbol . ":" where: {
@@ -734,7 +729,7 @@
 		if: (Parts length) = 0 {
 			Parts <- []
 		}
-		Initial | Parts foldr: #{
+		combined <- Initial | Parts foldr: #{
 			name <- ""
 			args <- []
 		} with: :acc el {
@@ -745,83 +740,31 @@
 			} else: {
 				nextArgs <- (el val) | nextArgs
 			}
-
 			#{
 				name <- nextName
 				args <- nextArgs
-				string <- {
-					str <- ""
-					curArgs <- args
-					nameParts <- name splitOn: ":"
-					foreach: nameParts :idx part {
-						str <- str . part . ":"
-						if: (not: (curArgs empty?)) {
-							str <- str . " " . (curArgs value)
-							curArgs <- curArgs tail
-						}
-					}
-					while: { not: (curArgs empty?) } do: {
-						str <- str . " " . (curArgs value)
-						curArgs <- curArgs tail
-					}
-					str
-				}
 			}
 		}
+		ast funcall: (ast symbol: (combined name)) withArgs: (combined args) hasReceiver?: false
 	}
 
 	unarymeth <- match: Receiver . hws . Method where: {
 		Receiver <- match: opexpr
 		Method <- match: symexpr
 	} yield: {
-		#{
-			receiver <- Receiver
-			name <- Method name
-			args <- []
-			string <- {
-				(string: receiver) . " " . name
-			}
-		}
+		ast funcall: Method withArgs: [Receiver] hasReceiver?: true
 	}
 
 	methcall <- match: Receiver . hws . Rest where: {
 		Receiver <- match: opexpr
 		Rest <- match: funcall
 	} yield: {
-		#{
-			receiver <- Receiver
-			name <- Rest name
-			args <- Rest args
-			string <- {
-				nameParts <- name splitOn: ":"
-				curArgs <- args
-				str <- (string: receiver) . " "
-				foreach: nameParts :part {
-					str <- str . part . ":"
-					if: (not: (curArgs empty?)) {
-						str <- str . " " . (curArgs value)
-						curArgs <- curArgs tail
-					}
-				}
-				while: { not: (curArgs empty?) } do: {
-					str <- str . " " . (curArgs value)
-					curArgs <- curArgs tail
-				}
-				str
-			}
-		}
+		ast funcall: (Rest tocall) withArgs: Receiver | (Rest args) hasReceiver?: true
 	}
 	_processOpPieces <- :Left Pieces {
 		if: (Pieces length) > 0 {
 			Pieces fold: Left with: :acc piece {
-				#{
-					left <- acc
-					op <- piece op
-					right <- piece right
-					string <- {
-						(string: left) . " " . op . " " . right
-					}
-				}
+				ast binaryOp: (piece op) withArgs: acc (piece right)
 			}
 		} else: {
 			Left
@@ -868,13 +811,7 @@
 		]
 		Expr <- match: expr
 	} yield: {
-		#{
-			assign <- Expr
-			to <- Symbol
-			string <- {
-				(string: to) . " <- " . assign
-			}
-		}
+		ast assign: Expr to: Symbol
 	}
 
 	object <- match: "#{" . ws . Messages . "}" where: {
@@ -888,14 +825,7 @@
 		if: (Messages length) = 0 {
 			Messages <- []
 		}
-		#{
-			messages <- Messages
-			string <- {
-				"#{\n\t". ((messages map: :el {
-					string: el
-				}) join: "\n\t") . "\n}"
-			}
-		}
+		ast object: Messages
 	}
 
 	listlit <- match: "[" . ws . Els . "]" where: {
@@ -905,14 +835,7 @@
 		if: (Els length) = 0 {
 			Els <- []
 		}
-		#{
-			litval <- Els
-			string <- {
-				"[\n\t". ((litval map: :el {
-					string: el
-				}) join: "\n\t") . "\n]"
-			}
-		}
+		ast seqLit: Els array?: false
 	}
 
 	arraylit <- match: "#[" . ws . Els . "]" where: {
@@ -922,14 +845,7 @@
 		if: (Els length) = 0 {
 			Els <- []
 		}
-		#{
-			litval <- Els
-			string <- {
-				"#[\n\t". ((litval map: :el {
-					string: el
-				}) join: "\n\t") . "\n]"
-			}
-		}
+		ast seqLit: Els array?: true
 	}
 
 	argname <- match: hws . Pre . Initial . Rest where: {
@@ -958,15 +874,7 @@
 		if: (Exprs length) = 0 {
 			Exprs <- []
 		}
-		#{
-			args <- Arglist
-			expressions <- Exprs
-			string <- {
-				(args join: " ") . "{\n\t" .
-					((expressions map: :el { string: el }) join: "\n\t") .
-				"}"
-			}
-		}
+		ast lambda: Exprs withArgs: Arglist
 	}
 
 	parenexp <- match: "(" . ws . Expr . ws . ")" where: {