Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move expressions to individual files, add pervasives, interpreter
- Loading branch information
Showing
23 changed files
with
447 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
zero-compiler/src/main/java/zero/compiler/parser/AExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* Copyright (C) 2012 Matt O'Connor <thegreendragon@gmail.com> */ | ||
package zero.compiler.parser; | ||
|
||
public abstract class AExpression implements Expression { | ||
@Override | ||
public String toString() { | ||
return toString(new StringBuilder()).toString(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
zero-compiler/src/main/java/zero/compiler/parser/ApplyExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* Copyright (C) 2012 Matt O'Connor <thegreendragon@gmail.com> */ | ||
package zero.compiler.parser; | ||
|
||
public class ApplyExpression extends AExpression { | ||
public final Expression fn; | ||
public final Expression[] params; | ||
|
||
public ApplyExpression(final Expression fn, final Expression... params) { | ||
this.fn = fn; | ||
this.params = params == null ? new Expression[0] : params; | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(ExpressionVisitor<R, C> v, C c) { | ||
return v.visit(this, c); | ||
} | ||
|
||
@Override | ||
public StringBuilder toString(final StringBuilder b) { | ||
b.append("(("); | ||
fn.toString(b); | ||
b.append(')'); | ||
for(final Expression param : params) { | ||
b.append(' '); | ||
param.toString(b); | ||
} | ||
b.append(')'); | ||
return b; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
zero-compiler/src/main/java/zero/compiler/parser/CaseExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* Copyright (C) 2012 Matt O'Connor <thegreendragon@gmail.com> */ | ||
package zero.compiler.parser; | ||
|
||
public class CaseExpression extends AExpression { | ||
public final PatternExpression bind; | ||
public final Expression result; | ||
|
||
public CaseExpression(final PatternExpression bind, final Expression result) { | ||
this.bind = bind; | ||
this.result = result; | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(ExpressionVisitor<R, C> v, C c) { | ||
return v.visit(this, c); | ||
} | ||
|
||
@Override | ||
public StringBuilder toString(final StringBuilder b) { | ||
if(bind.isWildcard()) { | ||
b.append("else "); | ||
} else { | ||
b.append("| "); | ||
bind.toString(b); | ||
b.append(" -> "); | ||
} | ||
result.toString(b); | ||
return b; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
zero-compiler/src/main/java/zero/compiler/parser/FnExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* Copyright (C) 2012 Matt O'Connor <thegreendragon@gmail.com> */ | ||
package zero.compiler.parser; | ||
|
||
public class FnExpression extends AExpression { | ||
public final NameExpression[] params; | ||
public final Expression body; | ||
|
||
public FnExpression(final Expression body, final NameExpression... params) { | ||
this.params = params == null ? new NameExpression[0] : params; | ||
this.body = body; | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(ExpressionVisitor<R, C> v, C c) { | ||
return v.visit(this, c); | ||
} | ||
|
||
@Override | ||
public StringBuilder toString(final StringBuilder b) { | ||
b.append("fn "); | ||
for(final NameExpression name : params) { | ||
name.toString(b).append(' '); | ||
} | ||
b.append("-> "); | ||
body.toString(b); | ||
return b; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
zero-compiler/src/main/java/zero/compiler/parser/IntegerExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* Copyright (C) 2012 Matt O'Connor <thegreendragon@gmail.com> */ | ||
package zero.compiler.parser; | ||
|
||
public class IntegerExpression extends AExpression { | ||
public final int val; | ||
|
||
public IntegerExpression(final Token token) { | ||
this.val = Integer.parseInt(token.getText()); | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(ExpressionVisitor<R, C> v, C c) { | ||
return v.visit(this, c); | ||
} | ||
|
||
@Override | ||
public StringBuilder toString(final StringBuilder b) { | ||
return b.append(val); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
zero-compiler/src/main/java/zero/compiler/parser/MatchExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* Copyright (C) 2012 Matt O'Connor <thegreendragon@gmail.com> */ | ||
package zero.compiler.parser; | ||
|
||
public class MatchExpression extends AExpression { | ||
public final Expression val; | ||
public final CaseExpression[] cases; | ||
|
||
public MatchExpression(final Expression val, final CaseExpression... cases) { | ||
this.val = val; | ||
this.cases = cases == null ? new CaseExpression[0] : cases; | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(ExpressionVisitor<R, C> v, C c) { | ||
return v.visit(this, c); | ||
} | ||
|
||
@Override | ||
public StringBuilder toString(final StringBuilder b) { | ||
b.append("match "); | ||
val.toString(b); | ||
b.append(" with "); | ||
for(final CaseExpression c : cases) { | ||
c.toString(b); | ||
b.append(' '); | ||
} | ||
b.append("end"); | ||
return b; | ||
} | ||
} |
Oops, something went wrong.