Skip to content

Commit

Permalink
Parse and evaluate files incrementally.
Browse files Browse the repository at this point in the history
This minor change has a big semantic effect. Top-level xpressions in
a file will now be *evaluated* before subsequent expressions are
*parsed*. This allows expressions that define parsers (or import
them) to affect the parsing of the rest of the file.
  • Loading branch information
munificent committed Feb 13, 2011
1 parent 38418d0 commit bffe492
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 37 deletions.
12 changes: 10 additions & 2 deletions src/com/stuffwithstuff/magpie/Script.java
Expand Up @@ -8,6 +8,7 @@
import java.nio.charset.Charset;
import java.util.List;

import com.stuffwithstuff.magpie.ast.Expr;
import com.stuffwithstuff.magpie.interpreter.CheckError;
import com.stuffwithstuff.magpie.interpreter.Checker;
import com.stuffwithstuff.magpie.interpreter.Interpreter;
Expand Down Expand Up @@ -44,8 +45,15 @@ public void execute(Interpreter interpreter) {
Lexer lexer = new Lexer(mPath, new StringCharacterReader(mText));
MagpieParser parser = interpreter.createParser(lexer);

interpreter.load(parser.parse());

// Evaluate every expression in the file. We do this incrementally so
// that expressions that define parsers can be used to parse the rest of
// the file.
while (true) {
Expr expr = parser.parseTopLevelExpression();
if (expr == null) break;
interpreter.interpret(expr);
}

// If there is a main() function, then we need to type-check first:
if (interpreter.hasMain()) {
// Do the static analysis and see if we got the errors we expect.
Expand Down
17 changes: 7 additions & 10 deletions src/com/stuffwithstuff/magpie/interpreter/Interpreter.java
Expand Up @@ -131,20 +131,17 @@ public Interpreter(InterpreterHost host) {
EnvironmentBuilder.initialize(this);
}

public void load(List<Expr> expressions) {
public void interpret(Expr expression) {
EvalContext context = createTopLevelContext();

// Evaluate the expressions. This is the load time evaluation.
for (Expr expr : expressions) {
try {
evaluate(expr, context);
} catch(ErrorException err) {
// TODO(bob): Better error message here!
mHost.runtimeError(expr.getPosition(), "Uncaught error.");
}
try {
evaluate(expression, context);
} catch(ErrorException err) {
// TODO(bob): Better error message here!
mHost.runtimeError(expression.getPosition(), "Uncaught error.");
}
}

public Obj evaluate(Expr expr) {
return evaluate(expr, createTopLevelContext());
}
Expand Down
Expand Up @@ -5,7 +5,6 @@
import java.util.List;

import com.stuffwithstuff.magpie.Script;
import com.stuffwithstuff.magpie.ast.BlockExpr;
import com.stuffwithstuff.magpie.ast.Expr;
import com.stuffwithstuff.magpie.interpreter.CheckError;
import com.stuffwithstuff.magpie.interpreter.Checker;
Expand Down Expand Up @@ -91,18 +90,8 @@ public Obj invoke(Interpreter interpreter, Obj thisObj, Obj arg) {
e.printStackTrace();
}

// Pull out the list of expressions. If it's a BlockExpr, we do this so that
// it doesn't evaluate the body in a nested scope.
List<Expr> exprs;
if (expr instanceof BlockExpr) {
exprs = ((BlockExpr)expr).getExpressions();
} else {
exprs = new ArrayList<Expr>();
exprs.add(expr);
}
inner.interpret(expr);

inner.load(exprs);

// Do the static analysis and see if we got the errors we expect.
Checker checker = new Checker(inner);
checker.checkAll();
Expand Down
19 changes: 6 additions & 13 deletions src/com/stuffwithstuff/magpie/parser/MagpieParser.java
Expand Up @@ -78,19 +78,12 @@ public MagpieParser(Lexer lexer) {
this(lexer, null, null, null);
}

public List<Expr> parse() {
// Parse the entire file.
List<Expr> expressions = new ArrayList<Expr>();
do {
expressions.add(parseExpression());

// Allow files with no trailing newline.
if (match(TokenType.EOF)) break;

consume(TokenType.LINE);
} while (!match(TokenType.EOF));

return expressions;
public Expr parseTopLevelExpression() {
if (lookAhead(TokenType.EOF)) return null;

Expr expr = parseExpression();
if (!lookAhead(TokenType.EOF)) consume(TokenType.LINE);
return expr;
}

public Expr parseExpression(int stickiness) {
Expand Down

0 comments on commit bffe492

Please sign in to comment.