Skip to content

Commit

Permalink
Merge pull request #39 from cpfr/pattern-matching
Browse files Browse the repository at this point in the history
Pattern matching
  • Loading branch information
lukasoyen committed Nov 3, 2015
2 parents 1a42c07 + 9052eda commit 77e31b7
Show file tree
Hide file tree
Showing 37 changed files with 1,119 additions and 99 deletions.
51 changes: 49 additions & 2 deletions src/main/antlr4/de/uni/bremen/monty/moco/antlr/Monty.g4
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ importLine
declaration
: independentDeclaration
| classDeclaration
| caseClassDeclaration
| generatorDeclaration
;

Expand All @@ -34,6 +35,14 @@ classDeclaration
Dedent
;

caseClassDeclaration
: 'case' 'class' type '(' (parameterListWithoutDefaults)? ')' ('inherits' typeList)?
(':' EndOfLine
Indent
(memberDeclaration+ | 'pass' EndOfLine)
Dedent)?
;

memberDeclaration
: accessModifier? independentDeclaration
| accessModifier? abstractMethodDeclaration
Expand Down Expand Up @@ -107,11 +116,11 @@ statementBlock
;

statement
: whileStatement #whileStm
: declaration #declStm
| whileStatement #whileStm
| forStatement #forStm
| ifStatement #ifStm
| tryStatement #tryStm
| declaration #declStm
| unpackAssignment #unpackAssignStm
| assignment #assignStm
| compoundAssignment #compoundAssign
Expand All @@ -122,6 +131,7 @@ statement
| command='break' EndOfLine #breakStm
| functionCall EndOfLine #funcCallStm
| left=expression operator='.' right=functionCall EndOfLine #MemberAccessStmt
| caseStatement #caseStm
;

/* while loop: The expression must be a condition (i.e. Boolean expression). */
Expand Down Expand Up @@ -296,4 +306,41 @@ listGenerator

listFilter
: 'if' expression
;


/* pattern matching */

caseStatement
: 'case' expression 'of' ':' EndOfLine caseBlock
;

caseBlock
: Indent
(pattern ':' EndOfLine statementBlock)+
Dedent
;

patternGuard
: 'if' expression
;

pattern
: (typedPattern
| '_'
| compoundPattern
| expression) patternGuard?
;

typedPattern
: type Identifier
| type ('_')?
;

compoundPattern
: type? '(' patternList? ')'
;

patternList
: pattern (',' pattern )*
;
2 changes: 1 addition & 1 deletion src/main/antlr4/imports/lex.g4
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ ClassIdentifier

Identifier
: '_'* LowercaseLetter (LowercaseLetter | UppercaseLetter | Digit | '_')*
| '_'+ (LowercaseLetter | UppercaseLetter | Digit | '_')*
| '_'+ (LowercaseLetter | UppercaseLetter | Digit | '_')+
;

ConstantIdentifier
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/de/uni/bremen/monty/moco/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@ public static void main(String[] args) throws IOException, InterruptedException
IOUtils.copy(Main.class.getResourceAsStream("/std_llvm_include.ll"), writer);

Package ast = buildPackage(inputFileName);
if (printAST) {
(new PrintVisitor()).visitDoubleDispatched(ast);
return;
}

if (!visitVisitors(ast, stopOnFirstError, writer.getBuffer())) {
return;
Expand Down
Loading

0 comments on commit 77e31b7

Please sign in to comment.