Initial thoughts and groundwork for scoping, activation records, and functions.
This commit is contained in:
		
							
								
								
									
										14
									
								
								src/edu/utexas/cs345/jdblisp/SpecialForms.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/edu/utexas/cs345/jdblisp/SpecialForms.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | |||||||
|  | package edu.utexas.cs345.jdblisp; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * SpecialForms | ||||||
|  |  * @author Jonathan Bernard (jdbernard@gmail.com) | ||||||
|  |  */ | ||||||
|  | public class SpecialForms { | ||||||
|  |  | ||||||
|  |     public static SymbolTable createTable() { | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static TableEntry  | ||||||
|  | } | ||||||
							
								
								
									
										52
									
								
								src/edu/utexas/cs345/jdblisp/SymbolTable.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								src/edu/utexas/cs345/jdblisp/SymbolTable.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | |||||||
|  | package edu.utexas.cs345.jdblisp; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * SymbolTable | ||||||
|  |  * @author Jonathan Bernard (jdbernard@gmail.com) | ||||||
|  |  */ | ||||||
|  | public class SymbolTable { | ||||||
|  |     public final TableEntry top; | ||||||
|  |     public final SymbolTable rest; | ||||||
|  |  | ||||||
|  |     public SymbolTable(TableEntry te, SymbolTable st) { | ||||||
|  |         this.top = te; | ||||||
|  |         this.rest = st; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public SymbolTable(TableEntry... te) { | ||||||
|  |         int idx = te.length; | ||||||
|  |         SymbolTable last = null; | ||||||
|  |         while (idx > 1) last = new SymbolTable(te[--idx], last); | ||||||
|  |  | ||||||
|  |         assert (--idx == 0) : "Error in SymbolTable construction. " | ||||||
|  |             + "Invalid ending index"; | ||||||
|  |  | ||||||
|  |         this.top = te[0]; | ||||||
|  |         this.rest = last; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public TableEntry findFunction(Symbol symbol) throws LispException { | ||||||
|  |         if (top.name.equals(symbol) && top.parameters != null) | ||||||
|  |             return top; | ||||||
|  |  | ||||||
|  |         if (rest == null) | ||||||
|  |             throw new LispException("Undefined function: " | ||||||
|  |                 + symbol.name); | ||||||
|  |  | ||||||
|  |         return rest.findFunction(symbol); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public TableEntry findVariable(Symbol symbol) throws LispException { | ||||||
|  |         // if this entry is the one we're looking for, return it | ||||||
|  |         if (top.name.equals(symbol) && top.parameters == null) | ||||||
|  |             return top; | ||||||
|  |  | ||||||
|  |         // if not, and there are no more entries | ||||||
|  |         if (rest == null) | ||||||
|  |             throw new LispException("Undefined variable: " | ||||||
|  |                 + symbol.name); | ||||||
|  |  | ||||||
|  |         // otherwise, search the remainder of the table | ||||||
|  |         return rest.findVariable(symbol); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										18
									
								
								src/edu/utexas/cs345/jdblisp/TableEntry.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/edu/utexas/cs345/jdblisp/TableEntry.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | package edu.utexas.cs345.jdblisp; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @author Jonathan Bernard (jdbernard@gmail.com) | ||||||
|  |  */ | ||||||
|  | public class TableEntry { | ||||||
|  |  | ||||||
|  |     public final Symbol name; | ||||||
|  |     public final Symbol[] parameters; | ||||||
|  |     public final SExp body; | ||||||
|  |  | ||||||
|  |     public TableEntry(Symbol name, Symbol[] parameters, SExp body) { | ||||||
|  |         this.name = name; | ||||||
|  |         this.parameters = parameters; | ||||||
|  |         this.body = body; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user