forked from manifold-lang/manifold-frontend
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from m-lyons/import
Imports are now expressions
- Loading branch information
Showing
26 changed files
with
434 additions
and
211 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
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
86 changes: 86 additions & 0 deletions
86
src/main/java/org/manifold/compiler/front/ExpressionGraphParser.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,86 @@ | ||
package org.manifold.compiler.front; | ||
|
||
import org.antlr.v4.runtime.*; | ||
import org.antlr.v4.runtime.atn.ATNConfigSet; | ||
import org.antlr.v4.runtime.dfa.DFA; | ||
import org.antlr.v4.runtime.misc.NotNull; | ||
import org.antlr.v4.runtime.misc.Nullable; | ||
import org.apache.log4j.LogManager; | ||
import org.apache.log4j.Logger; | ||
import org.manifold.parser.ManifoldLexer; | ||
import org.manifold.parser.ManifoldParser; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.BitSet; | ||
import java.util.List; | ||
|
||
public class ExpressionGraphParser { | ||
|
||
private static Logger log = LogManager.getLogger("ExpressionGraphParser"); | ||
|
||
public ExpressionGraph parseFile(File inputFile) throws IOException { | ||
ManifoldLexer lexer = new ManifoldLexer(new ANTLRInputStream( | ||
new FileInputStream(inputFile))); | ||
|
||
// Get a list of matched tokens | ||
CommonTokenStream tokens = new CommonTokenStream(lexer); | ||
|
||
// Pass the tokens to the parser | ||
ManifoldParser parser = new ManifoldParser(tokens); | ||
|
||
StringBuilder errors = new StringBuilder(); | ||
parser.addErrorListener(new ANTLRErrorListener() { | ||
|
||
@Override | ||
public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, | ||
int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) { | ||
errors.append("Error at line ").append(line).append(", char ") | ||
.append(charPositionInLine).append(": ").append(msg).append("\n"); | ||
} | ||
|
||
@Override | ||
public void reportAmbiguity(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, | ||
boolean exact, @Nullable BitSet ambigAlts, @NotNull ATNConfigSet configs) { | ||
// Pass | ||
} | ||
|
||
@Override | ||
public void reportAttemptingFullContext(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, | ||
int stopIndex, @Nullable BitSet conflictingAlts, | ||
@NotNull ATNConfigSet configs) { | ||
// Pass | ||
} | ||
|
||
@Override | ||
public void reportContextSensitivity(@NotNull Parser recognizer, @NotNull DFA dfa, int startIndex, int stopIndex, | ||
int prediction, @NotNull ATNConfigSet configs) { | ||
// Pass | ||
} | ||
}); | ||
|
||
// Specify our entry point | ||
ManifoldParser.SchematicContext context = parser.schematic(); | ||
|
||
if (errors.length() != 0) { | ||
throw new FrontendBuildException(errors.toString()); | ||
} | ||
|
||
ExpressionContextVisitor graphBuilder = new ExpressionContextVisitor(inputFile); | ||
List<ManifoldParser.ExpressionContext> expressionContexts = context.expression(); | ||
for (ManifoldParser.ExpressionContext expressionContext : expressionContexts) { | ||
graphBuilder.visit(expressionContext); | ||
} | ||
if (graphBuilder.getErrors().size() > 0) { | ||
throw new FrontendBuildException( | ||
String.join("\n", graphBuilder.getErrors())); | ||
} | ||
ExpressionGraph exprGraph = graphBuilder.getExpressionGraph(); | ||
|
||
log.debug("writing out initial expression graph"); | ||
File exprGraphDot = new File(inputFile.getName() + ".exprs.dot"); | ||
exprGraph.writeDOTFile(exprGraphDot); | ||
return exprGraph; | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/main/java/org/manifold/compiler/front/ImportTypeValue.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,29 @@ | ||
package org.manifold.compiler.front; | ||
|
||
import org.manifold.compiler.SchematicValueVisitor; | ||
import org.manifold.compiler.TypeValue; | ||
import org.manifold.compiler.UndefinedBehaviourError; | ||
|
||
public class ImportTypeValue extends TypeValue implements NamedEntryTypeValue { | ||
|
||
private NamespaceIdentifier namespace; | ||
private ExpressionGraph exprGraph; | ||
|
||
public ImportTypeValue(ExpressionGraph exprGraph, NamespaceIdentifier namespace) { | ||
this.exprGraph = exprGraph; | ||
this.namespace = namespace; | ||
} | ||
|
||
public TypeValue getEntry(String key) throws Exception { | ||
VariableIdentifier id = new VariableIdentifier(namespace, key); | ||
VariableReferenceVertex source = exprGraph.getVariableVertex(id); | ||
source.elaborate(); | ||
return source.getType(); | ||
} | ||
|
||
@Override | ||
public void accept(SchematicValueVisitor schematicValueVisitor) { | ||
throw new UndefinedBehaviourError( | ||
"cannot accept non-frontend ValueVisitor into a frontend Value"); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/org/manifold/compiler/front/ImportValue.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,37 @@ | ||
package org.manifold.compiler.front; | ||
|
||
import org.manifold.compiler.SchematicValueVisitor; | ||
import org.manifold.compiler.Value; | ||
|
||
public class ImportValue extends Value implements NamedEntryValue { | ||
private ExpressionGraph exprGraph; | ||
private NamespaceIdentifier namespace; | ||
|
||
public ImportValue(ExpressionGraph exprGraph, NamespaceIdentifier namespace) { | ||
super(new ImportTypeValue(exprGraph, namespace)); | ||
this.exprGraph = exprGraph; | ||
this.namespace = namespace; | ||
} | ||
@Override | ||
public boolean isElaborationtimeKnowable() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isRuntimeKnowable() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void accept(SchematicValueVisitor schematicValueVisitor) { | ||
return; | ||
} | ||
|
||
@Override | ||
public Value getEntry(String key) throws Exception { | ||
VariableIdentifier id = new VariableIdentifier(namespace, key); | ||
VariableReferenceVertex source = exprGraph.getVariableVertex(id); | ||
source.elaborate(); | ||
return source.getValue(); | ||
} | ||
} |
Oops, something went wrong.