changeset 252:004946743678

Added code for building a method symbol table
author Michael Pavone <pavone@retrodev.com>
date Sat, 10 May 2014 19:11:01 -0700
parents 2557ce4e671f
children 697c2c562af2 fb922651db29
files modules/ast.tp modules/compiler.tp modules/parser.tp modules/symbols.tp
diffstat 4 files changed, 140 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/modules/ast.tp	Fri Apr 11 22:29:32 2014 -0700
+++ b/modules/ast.tp	Sat May 10 19:11:01 2014 -0700
@@ -36,6 +36,11 @@
 				string <- {
 					stringIndent: ""
 				}
+				fold:with <- :acc :fun {
+					acc <- fun: acc self
+					acc <- _left fold: acc with: fun
+					_right fold: acc with: fun
+				}
 			}
 		}
 
@@ -49,6 +54,9 @@
 				string <- {
 					stringIndent: ""
 				}
+				fold:with <- :acc :fun {
+					fun: acc self
+				}
 			}
 		}
 
@@ -89,6 +97,9 @@
 				string <- {
 					stringIndent: ""
 				}
+				fold:with <- :acc :fun {
+					fun: acc self
+				}
 			}
 		}
 
@@ -102,11 +113,15 @@
 				string <- {
 					stringIndent: ""
 				}
+				fold:with <- :acc :fun {
+					fun: acc self
+				}
 			}
 		}
 
 		funcall:withArgs:hasReceiver? <- :_tocall :_args :_receiver? {
 			#{
+				nodeType <- { _call }
 				tocall <- _tocall
 				args <- _args
 				hasReceiver? <- _receiver?
@@ -139,6 +154,12 @@
 				string <- {
 					stringIndent: ""
 				}
+				fold:with <- :acc :fun {
+					acc <- fun: acc self
+					_args fold: acc with: :acc el {
+						fun: acc el
+					}
+				}
 			}
 		}
 
@@ -155,6 +176,12 @@
 				string <- {
 					stringIndent: ""
 				}
+				fold:with <- :acc :fun {
+					acc <- fun: acc self
+					messages fold: acc with: :acc el {
+						fun: acc el
+					}
+				}
 			}
 		}
 
@@ -172,6 +199,12 @@
 				string <- {
 					stringIndent: ""
 				}
+				fold:with <- :acc :fun {
+					acc <- fun: acc self
+					els fold: acc with: :acc el {
+						fun: acc el
+					}
+				}
 			}
 		}
 
@@ -186,6 +219,11 @@
 				string <- {
 					stringIndent: ""
 				}
+				fold:with <- :acc :fun {
+					acc <- fun: acc self
+					acc <- fun: acc _sym
+					fun: acc _expr
+				}
 			}
 		}
 
@@ -207,6 +245,12 @@
 				string <- {
 					stringIndent: ""
 				}
+				fold:with <- :acc :fun {
+					acc <- fun: acc self
+					expressions fold: acc with: :acc el {
+						fun: acc el
+					}
+				}
 			}
 		}
 	}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/compiler.tp	Sat May 10 19:11:01 2014 -0700
@@ -0,0 +1,25 @@
+#{
+	main <- :args {
+		if: (args length) > 1 {
+			file <- os open: (args get: 1) (os O_RDONLY)
+
+			code <- ""
+			chunksize <- 1024
+			readsize <- chunksize
+			while: { readsize = chunksize} do: {
+				seg <- os read: file chunksize
+				code <- code . seg
+				readsize <- seg byte_length
+			}
+			res <- parser top: code
+			if: res {
+				methods <- symbols buildMethodTable: (res yield)
+				print: methods
+			} else: {
+				print: "Parse failed!\n"
+			}
+		} else: {
+			print: "Usage compiler FILE\n"
+		}
+	}
+}
--- a/modules/parser.tp	Fri Apr 11 22:29:32 2014 -0700
+++ b/modules/parser.tp	Sat May 10 19:11:01 2014 -0700
@@ -12,6 +12,7 @@
 		byte_length <- { _len }
 		string <- {
 			if: _needsflat? {
+				_needsflat? <- false
 				_flat <- _base from: _start withLength: _len
 			}
 			_flat
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/symbols.tp	Sat May 10 19:11:01 2014 -0700
@@ -0,0 +1,70 @@
+{
+	_null <- #{
+		find:else <- :_ :else {
+			else:
+		}
+	}
+	_local <- 0
+	_closedover <- 1
+	_upvar <- 2
+	_method <- 3
+	_self <- 4
+	_parent <- 5
+
+	_nextMethodId <- 0
+	_method <- :_name {
+		_id <- _nextMethodId
+		_nextMethodId <- _id + 1
+		#{
+			name <- { _name }
+			id <- { _id }
+			string <- { "method " . _name . "(" . _id . ")" }
+		}
+	}
+	#{
+		nullTable <- { _null }
+
+		tablewithParent <- :_parent {
+			_symbols <- dict hash
+			#{
+				find:else <- :name :else {
+					_symbols get: name else: {
+						_parent find: name else: else
+					}
+				}
+				defineMethod <- :name {
+					_symbols get: name else: {
+						_symbols set: name (_method: name)
+					}
+					self
+				}
+				print <- {
+					foreach: _symbols :name info {
+						print: name . ": " . info . "\n"
+					}
+				}
+			}
+		}
+
+		table <- {
+			tablewithParent: _null
+		}
+
+		buildMethodTable <- :tree {
+			_object <- ast obj
+			_assignment <- ast assignment
+			tree fold: table with: :acc el {
+				if: (el nodeType) = _object {
+					(el messages) fold: acc with: :acc msg {
+						if: (msg nodeType) = _assignment {
+							acc defineMethod: ((msg to) name)
+						}
+						acc
+					}
+				} else: {
+					acc
+				}
+			}
+		}
+	}
+}