added lexer for Core language, removed Eclipse/VSCode settings

This commit is contained in:
User 2019-02-07 01:55:06 +01:00
parent 6718a77890
commit 8c65fe71cf
13 changed files with 198 additions and 28 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>boppi</name>
<name>boppi2</name>
<comment></comment>
<projects>
</projects>

View File

@ -1,2 +0,0 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@ -1,2 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false

View File

@ -1,14 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.5

View File

@ -1,4 +0,0 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

View File

@ -1,3 +0,0 @@
{
"java.configuration.updateBuildConfiguration": "disabled"
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." name="boppi">
<project basedir="." name="boppi2">
<property environment="env" />
<property name="junit.output.dir" value="doc/junit" />
<property name="g4.iloc" value="src/pp/iloc/parse" />
@ -106,6 +106,18 @@
<arg value="-no-listener" />
<arg value="-visitor" />
</java>
<java classname="org.antlr.v4.Tool">
<classpath>
<pathelement location="lib/antlr-4.7-complete.jar" />
</classpath>
<arg value="${g4.boppi}/BoppiCore.g4" />
<arg value="-o" />
<arg value="${g4.boppi}" />
<arg value="-package" />
<arg value="pp.s1184725.boppi.antlr" />
<arg value="-listener" />
<arg value="-no-visitor" />
</java>
<java classname="org.antlr.v4.Tool">
<classpath>
<pathelement location="lib/antlr-4.7-complete.jar" />

View File

@ -13,7 +13,6 @@ import pp.iloc.model.Num;
import pp.iloc.model.Op;
import pp.iloc.model.OpClaz;
import pp.iloc.model.OpCode;
import pp.iloc.model.Operand.Type;
import pp.iloc.model.Program;
import pp.iloc.parse.FormatException;

View File

@ -0,0 +1,114 @@
package pp.s1184725.boppi;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import pp.s1184725.boppi.antlr.*;
import pp.s1184725.boppi.antlr.BoppiCoreParser.*;
import pp.s1184725.boppi.exception.*;
import pp.s1184725.boppi.type.*;
/**
* This class performs type checking and variable assignment on a bare parse
* tree.
*
* @author Frank Wibbelink
*/
public class BoppiCoreChecker extends BoppiCoreBaseListener {
public static void main(String[] args) {
BoppiCoreLexer lexer = new BoppiCoreLexer(ToolChain.getCharStream(BoppiCoreChecker.class, "test/test2.core"));
BoppiCoreParser parser = new BoppiCoreParser(new CommonTokenStream(lexer));
ParseTree ast = parser.program();
Logger logger = Logger.getLogger("TEST");
Annotations annotations = checkProgram(ast, logger);
System.out.println(ToolChain.getAnnotatedDOT(ast, annotations));
}
private Annotations an;
private Logger log;
/**
* Checks and annotates a program. Problems are reported to the given
* logger.
*
* @param ast
* the program's AST
* @param logger
* the logger object to write warnings and errors to
* @return the generated annotations
*/
public static Annotations checkProgram(ParseTree ast, Logger logger) {
BoppiCoreChecker checker = new BoppiCoreChecker(logger, new Annotations());
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(checker, ast);
return checker.an;
}
protected BoppiCoreChecker(Logger logger, Annotations annotations) {
an = annotations;
log = logger;
}
/**
* Checks whether two types are compatible and log an error if they do not.
*
* @param type1
* the left hand or actual type
* @param type2
* the right hand or desired type
* @param node
* the context used for logging
*/
private void checkConstraint(Type type1, Type type2, ParserRuleContext node) {
if (!type2.equals(type1))
log.severe(getError(node, Messages.getString("BoppiChecker.0"), type1.toString(), type2.toString())); //$NON-NLS-1$
}
/**
* Returns an error message for a given parse tree node.
*
* @param ctx
* the parse tree node at which the error occurred
* @param message
* the error message
* @param args
* arguments for the message, see {@link String#format}
*/
private String getError(ParserRuleContext node, String message, Object... args) {
int line = node.getStart().getLine();
int column = node.getStart().getCharPositionInLine();
return String.format(Messages.getString("BoppiChecker.1"), line, column, String.format(message, args)); //$NON-NLS-1$
}
/**
* Returns whether the current context resides within a given rule context
* within a set root (may be {@code null}). Effectively traverses between
* {@code current} and {@code root} in the parent hierarchy looking for a
* {@code rule} class.
*
* @param <T>
* the rule class
* @param ctx
* the node to start traversing
* @param root
* the node to stop traversing
* @param rule
* the ParserRuleContext class to look for
* @return whether a matching rule is found between the current and root
* node
*/
private <T extends ParserRuleContext> boolean isWithinRule(ParserRuleContext ctx, ParserRuleContext root,
Class<T> rule) {
for (ParserRuleContext node = ctx; node != root && node != null; node = node.getParent())
if (node.getClass().isAssignableFrom(rule))
return true;
return false;
}
}

View File

@ -0,0 +1,34 @@
grammar BoppiCore;
import BoppiCoreTokens;
program: expr EOF;
block: BLOCKOPEN ((data|let) DELIM)+ BLOCKCLOSE;
data: DATA id EQUALS BLOCKOPEN (id DELIM)+ BLOCKCLOSE;
let: LET id EQUALS expr;
id: IDENTIFIER (HASTYPE aexpr)?;
expr
: expr expr #eApp
| LAMBDA id expr #eAbs
| PI id expr #eQuant
| block expr #eDecl
| CASE aexpr BLOCKOPEN (alt DELIM)+ BLOCKCLOSE #eCase
| aexpr #eSimple
;
alt: id TO expr;
aexpr
: id
| literal
| PAROPEN expr PARCLOSE
;
literal
: LITERALINT
| LITERALCHAR
;

View File

@ -0,0 +1,23 @@
lexer grammar BoppiCoreTokens;
DELIM: ';';
HASTYPE: ':';
LAMBDA: '\u03BB';
PI: '\u03A0';
PAROPEN: '(';
PARCLOSE: ')';
DATA: 'data';
LET: 'let';
IN: 'in';
CASE: 'case';
TO: '\u2192';
EQUALS: '=';
BLOCKOPEN: '{';
BLOCKCLOSE: '}';
LITERALINT: '0' | [1-9][0-9]*;
LITERALCHAR: '\'' . '\'';
IDENTIFIER: [\p{Alphabetic}]+ | '_' | '\u2605';
WHITESPACE: [\p{White_Space}] -> skip;
BLOCKCOMMENT: '/*' .*? '*/' -> skip;

View File

@ -0,0 +1 @@
5

View File

@ -0,0 +1,12 @@
{
data Boolean:★ = {
True;
False;
};
data List:(Π _:★ ★) = {
Cons : (Π a:★ Π _:a Π _:(List a) List a);
Nil : (Π a:★ List a);
};
let main = True;
}
main