First public commit!
This commit is contained in:
30
.gitignore
vendored
Normal file
30
.gitignore
vendored
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
#ignore thumbnails created by windows
|
||||||
|
Thumbs.db
|
||||||
|
#Ignore files build by Visual Studio
|
||||||
|
*.obj
|
||||||
|
*.exe
|
||||||
|
*.pdb
|
||||||
|
*.user
|
||||||
|
*.aps
|
||||||
|
*.pch
|
||||||
|
*.vspscc
|
||||||
|
*_i.c
|
||||||
|
*_p.c
|
||||||
|
*.ncb
|
||||||
|
*.suo
|
||||||
|
*.tlb
|
||||||
|
*.tlh
|
||||||
|
*.bak
|
||||||
|
*.cache
|
||||||
|
*.ilk
|
||||||
|
*.log
|
||||||
|
[Bb]in
|
||||||
|
[Dd]ebug*/
|
||||||
|
*.lib
|
||||||
|
*.sbr
|
||||||
|
obj/
|
||||||
|
[Rr]elease*/
|
||||||
|
_ReSharper*/
|
||||||
|
[Tt]est[Rr]esult*
|
||||||
|
output/
|
||||||
427
Grammar/tiger.g
Normal file
427
Grammar/tiger.g
Normal file
@@ -0,0 +1,427 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
grammar tiger;
|
||||||
|
|
||||||
|
options
|
||||||
|
{
|
||||||
|
language = CSharp3;
|
||||||
|
//language = Java;
|
||||||
|
output = AST;
|
||||||
|
k = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
tokens
|
||||||
|
{
|
||||||
|
// para uso del tree adaptor
|
||||||
|
ALIAS_DECL;
|
||||||
|
ARRAY_ACCESS;
|
||||||
|
ARRAY_DECL;
|
||||||
|
ARRAY_INST;
|
||||||
|
BREAK;
|
||||||
|
EXPR_SEQ;
|
||||||
|
FIELD_ACCESS;
|
||||||
|
FIELD_ACCESS_TERMINAL;
|
||||||
|
FIELD_INST;
|
||||||
|
FILL_IN_TYPE;
|
||||||
|
FOR;
|
||||||
|
FUN_CALL;
|
||||||
|
FUN_DECL;
|
||||||
|
FUN_DECL_SEQ;
|
||||||
|
IF;
|
||||||
|
LET;
|
||||||
|
NEG;
|
||||||
|
NIL;
|
||||||
|
PROGRAM;
|
||||||
|
RECORD_DECL;
|
||||||
|
RECORD_INST;
|
||||||
|
TYPE;
|
||||||
|
TYPE_DECL;
|
||||||
|
TYPE_DECL_SEQ;
|
||||||
|
TYPE_FIELD;
|
||||||
|
VAR_ACCESS;
|
||||||
|
VAR_DECL;
|
||||||
|
WHILE;
|
||||||
|
|
||||||
|
// para vista
|
||||||
|
ARGS_FIELDS;
|
||||||
|
DECL_BLOCK;
|
||||||
|
FIELDS_INST;
|
||||||
|
FUN_TYPE_WRAPPER;
|
||||||
|
PARAM_DECL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@lexer::header
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
}
|
||||||
|
|
||||||
|
@lexer::namespace{YATC.Grammar}
|
||||||
|
|
||||||
|
@lexer::modifier{public}
|
||||||
|
|
||||||
|
@lexer::ctorModifier{public}
|
||||||
|
|
||||||
|
@lexer::members
|
||||||
|
{
|
||||||
|
public override void ReportError(RecognitionException exc)
|
||||||
|
{
|
||||||
|
/* Abort on first error. */
|
||||||
|
throw new ParsingException(GetErrorMessage(exc, TokenNames), exc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@parser::header
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
}
|
||||||
|
|
||||||
|
@parser::namespace{YATC.Grammar}
|
||||||
|
|
||||||
|
@parser::modifier{public}
|
||||||
|
|
||||||
|
@parser::ctorModifier{public}
|
||||||
|
|
||||||
|
@parser::members
|
||||||
|
{
|
||||||
|
public override void ReportError(RecognitionException exc)
|
||||||
|
{
|
||||||
|
/* Abort on first error. */
|
||||||
|
throw new ParsingException(GetErrorMessage(exc, TokenNames), exc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment BEGIN_COMMENT
|
||||||
|
: '/*'
|
||||||
|
;
|
||||||
|
|
||||||
|
fragment END_COMMENT
|
||||||
|
: '*/'
|
||||||
|
;
|
||||||
|
|
||||||
|
// binary ops
|
||||||
|
PLUS : '+' ;
|
||||||
|
MINUS : '-' ;
|
||||||
|
MULT : '*' ;
|
||||||
|
DIV : '/' ;
|
||||||
|
|
||||||
|
// binary comparison
|
||||||
|
EQ : '=' ;
|
||||||
|
NOTEQ : '<>' ;
|
||||||
|
GT : '>' ;
|
||||||
|
GTEQ : '>=' ;
|
||||||
|
LT : '<' ;
|
||||||
|
LTEQ : '<=' ;
|
||||||
|
|
||||||
|
// logical ops
|
||||||
|
AND : '&' ;
|
||||||
|
OR : '|' ;
|
||||||
|
|
||||||
|
// grouping symbols
|
||||||
|
LPAREN : '(' ;
|
||||||
|
RPAREN : ')' ;
|
||||||
|
LBRACKET : '[' ;
|
||||||
|
RBRACKET : ']' ;
|
||||||
|
LKEY : '{' ;
|
||||||
|
RKEY : '}' ;
|
||||||
|
|
||||||
|
// separators
|
||||||
|
COMMA : ',' ;
|
||||||
|
SEMI : ';' ;
|
||||||
|
COLON : ':' ;
|
||||||
|
DOT : '.' ;
|
||||||
|
fragment
|
||||||
|
QUOTE : '\"';
|
||||||
|
|
||||||
|
ASSIGN : ':=' ;
|
||||||
|
|
||||||
|
// keywords
|
||||||
|
ARRAYKEY : 'array';
|
||||||
|
BREAKKEY : 'break';
|
||||||
|
DOKEY : 'do';
|
||||||
|
ELSEKEY : 'else';
|
||||||
|
ENDKEY : 'end';
|
||||||
|
FORKEY : 'for';
|
||||||
|
FUNCTIONKEY : 'function';
|
||||||
|
IFKEY : 'if';
|
||||||
|
INKEY : 'in';
|
||||||
|
INTKEY : 'int';
|
||||||
|
LETKEY : 'let';
|
||||||
|
NILKEY : 'nil';
|
||||||
|
OFKEY : 'of';
|
||||||
|
STRINGKEY : 'string';
|
||||||
|
THENKEY : 'then';
|
||||||
|
TOKEY : 'to';
|
||||||
|
TYPEKEY : 'type';
|
||||||
|
VARKEY : 'var';
|
||||||
|
WHILEKEY : 'while';
|
||||||
|
|
||||||
|
fragment
|
||||||
|
DIGIT
|
||||||
|
: '0'..'9'
|
||||||
|
;
|
||||||
|
|
||||||
|
fragment
|
||||||
|
LETTER
|
||||||
|
: 'a'..'z'|'A'..'Z'
|
||||||
|
;
|
||||||
|
|
||||||
|
fragment
|
||||||
|
ASCII_ESC
|
||||||
|
: '12' '0'..'7'
|
||||||
|
| '1' '0'..'1' '0'..'9'
|
||||||
|
| '0' '0'..'9' '0'..'9'
|
||||||
|
;
|
||||||
|
|
||||||
|
INT
|
||||||
|
: DIGIT+
|
||||||
|
;
|
||||||
|
|
||||||
|
ID : LETTER ( LETTER | DIGIT | '_' ) *
|
||||||
|
;
|
||||||
|
|
||||||
|
WS
|
||||||
|
: ( ' '|'\t'|'\r'|'\n' ) +
|
||||||
|
{$channel = Hidden;}
|
||||||
|
//{$channel = HIDDEN;}
|
||||||
|
;
|
||||||
|
|
||||||
|
fragment
|
||||||
|
ESC_SEQ
|
||||||
|
: '\\' ( 'n' | 'r' | 't' | QUOTE | ASCII_ESC | WS '\\' )
|
||||||
|
;
|
||||||
|
|
||||||
|
fragment
|
||||||
|
PRINTABLE_CHARACTER
|
||||||
|
: ((' '..'!') | ('#'.. '[') | (']'..'~'))
|
||||||
|
;
|
||||||
|
|
||||||
|
STRING : QUOTE ( ESC_SEQ | PRINTABLE_CHARACTER )* QUOTE ;
|
||||||
|
|
||||||
|
COMMENTARY
|
||||||
|
: BEGIN_COMMENT
|
||||||
|
( options {greedy=false;} : . )*
|
||||||
|
( COMMENTARY ( options {greedy=false;} : . )* )*
|
||||||
|
END_COMMENT
|
||||||
|
{$channel = Hidden;}
|
||||||
|
//{$channel = HIDDEN;}
|
||||||
|
;
|
||||||
|
|
||||||
|
public a_program
|
||||||
|
: expr ? EOF
|
||||||
|
-> ^(PROGRAM expr ?) ;
|
||||||
|
|
||||||
|
expr
|
||||||
|
: (ID LBRACKET disjunction_expr RBRACKET OFKEY) => array_inst
|
||||||
|
| (lvalue ASSIGN) => assignment
|
||||||
|
| disjunction_expr
|
||||||
|
| record_inst
|
||||||
|
| while_stat
|
||||||
|
| BREAKKEY
|
||||||
|
-> BREAK
|
||||||
|
;
|
||||||
|
|
||||||
|
disjunction_expr
|
||||||
|
: conjunction_expr (OR^ conjunction_expr)*
|
||||||
|
;
|
||||||
|
|
||||||
|
assignment
|
||||||
|
: lvalue ASSIGN^
|
||||||
|
( (ID LBRACKET disjunction_expr RBRACKET OFKEY) => array_inst
|
||||||
|
| disjunction_expr
|
||||||
|
| record_inst
|
||||||
|
)
|
||||||
|
;
|
||||||
|
|
||||||
|
record_inst
|
||||||
|
: ID LKEY field_inst_list? RKEY
|
||||||
|
-> ^(RECORD_INST ID field_inst_list?)
|
||||||
|
;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Queremos que en un for solo se puedan evaluar condiciones retornen valor,
|
||||||
|
por ello que pedimos que la primera y segunda expresiones sean disjunction_expr,
|
||||||
|
para evitar de que no devuelva por otros motivos.
|
||||||
|
*/
|
||||||
|
for_expr
|
||||||
|
: FORKEY type_id ASSIGN disjunction_expr TOKEY disjunction_expr DOKEY expr
|
||||||
|
-> ^(FOR type_id disjunction_expr disjunction_expr expr)
|
||||||
|
;
|
||||||
|
|
||||||
|
array_inst
|
||||||
|
: ID LBRACKET disjunction_expr RBRACKET OFKEY expr
|
||||||
|
-> ^(ARRAY_INST ID disjunction_expr expr)
|
||||||
|
;
|
||||||
|
|
||||||
|
conjunction_expr
|
||||||
|
: relational_expr (AND^ relational_expr)*
|
||||||
|
;
|
||||||
|
|
||||||
|
/*
|
||||||
|
El operador ? al final de la expresion le da la no asociatividad requerida
|
||||||
|
a los operadores de comparacion (notar que en los demas se usa * para
|
||||||
|
anidarlos en una lista). Esto es que no se permite a = b = c, pero si a = (b = c).
|
||||||
|
*/
|
||||||
|
relational_expr
|
||||||
|
: arith_expr ((EQ | NOTEQ | GT | LT | GTEQ | LTEQ )^ arith_expr)?
|
||||||
|
;
|
||||||
|
|
||||||
|
arith_expr
|
||||||
|
: term_expr ((PLUS | MINUS)^ term_expr)*
|
||||||
|
;
|
||||||
|
|
||||||
|
term_expr
|
||||||
|
: atom ((MULT | DIV)^ atom)*
|
||||||
|
;
|
||||||
|
|
||||||
|
field_inst_list
|
||||||
|
: record_field_inst ( COMMA record_field_inst )*
|
||||||
|
-> ^(FIELDS_INST record_field_inst+)
|
||||||
|
;
|
||||||
|
|
||||||
|
record_field_inst
|
||||||
|
: ID EQ expr
|
||||||
|
-> ^(FIELD_INST ID expr)
|
||||||
|
;
|
||||||
|
|
||||||
|
decl
|
||||||
|
: type_decl+
|
||||||
|
-> ^(TYPE_DECL_SEQ type_decl+)
|
||||||
|
| var_decl
|
||||||
|
| fun_decl+
|
||||||
|
-> ^(FUN_DECL_SEQ fun_decl+)
|
||||||
|
;
|
||||||
|
|
||||||
|
type_decl
|
||||||
|
: TYPEKEY ID EQ type
|
||||||
|
-> ^(TYPE_DECL ID type)
|
||||||
|
;
|
||||||
|
|
||||||
|
var_decl
|
||||||
|
: VARKEY type_id COLON type_id ASSIGN expr
|
||||||
|
-> ^(VAR_DECL type_id type_id expr)
|
||||||
|
| VARKEY type_id ASSIGN expr
|
||||||
|
-> ^(VAR_DECL type_id FILL_IN_TYPE expr)
|
||||||
|
;
|
||||||
|
|
||||||
|
fun_decl
|
||||||
|
: FUNCTIONKEY type_id LPAREN type_fields? RPAREN (COLON type_id)? EQ expr
|
||||||
|
-> ^(FUN_DECL type_id ^(PARAM_DECL type_fields?) ^(FUN_TYPE_WRAPPER type_id?) expr)
|
||||||
|
;
|
||||||
|
|
||||||
|
type_id
|
||||||
|
: STRINGKEY
|
||||||
|
| INTKEY
|
||||||
|
| ID
|
||||||
|
;
|
||||||
|
|
||||||
|
type
|
||||||
|
: type_id
|
||||||
|
-> ^(ALIAS_DECL type_id)
|
||||||
|
| array_decl
|
||||||
|
| record_decl
|
||||||
|
;
|
||||||
|
|
||||||
|
record_decl
|
||||||
|
: LKEY type_fields? RKEY
|
||||||
|
-> ^(RECORD_DECL type_fields?)
|
||||||
|
;
|
||||||
|
|
||||||
|
array_decl
|
||||||
|
: ARRAYKEY OFKEY type_id
|
||||||
|
-> ^(ARRAY_DECL type_id)
|
||||||
|
;
|
||||||
|
|
||||||
|
type_fields
|
||||||
|
: type_field (COMMA type_field)*
|
||||||
|
-> type_field+
|
||||||
|
;
|
||||||
|
|
||||||
|
type_field
|
||||||
|
: type_id COLON type_id
|
||||||
|
-> ^(TYPE_FIELD type_id type_id)
|
||||||
|
;
|
||||||
|
|
||||||
|
atom
|
||||||
|
: MINUS atom
|
||||||
|
-> ^(NEG atom)
|
||||||
|
| constant_value
|
||||||
|
| lvalue
|
||||||
|
| funcall
|
||||||
|
| if_then_expr
|
||||||
|
| for_expr
|
||||||
|
| let_expr
|
||||||
|
| LPAREN expr_seq RPAREN
|
||||||
|
-> expr_seq
|
||||||
|
;
|
||||||
|
|
||||||
|
constant_value
|
||||||
|
: STRING
|
||||||
|
| INT
|
||||||
|
| NILKEY
|
||||||
|
;
|
||||||
|
|
||||||
|
if_then_expr
|
||||||
|
: IFKEY conditional=disjunction_expr THENKEY then_expr=expr (ELSEKEY else_expr=expr)?
|
||||||
|
-> ^(IF $conditional $then_expr $else_expr?)
|
||||||
|
;
|
||||||
|
|
||||||
|
while_stat
|
||||||
|
: WHILEKEY cond=disjunction_expr DOKEY do_expr=expr
|
||||||
|
-> ^(WHILE $cond $do_expr)
|
||||||
|
;
|
||||||
|
|
||||||
|
let_expr
|
||||||
|
: LETKEY decl+ INKEY expr_seq ENDKEY
|
||||||
|
-> ^(LET ^(DECL_BLOCK decl+) expr_seq)
|
||||||
|
;
|
||||||
|
|
||||||
|
lvalue
|
||||||
|
: type_id lvalue_access? -> ^(VAR_ACCESS type_id lvalue_access?)
|
||||||
|
;
|
||||||
|
|
||||||
|
lvalue_access
|
||||||
|
: DOT type_id lvalue_access?
|
||||||
|
-> ^(FIELD_ACCESS type_id lvalue_access?)
|
||||||
|
| LBRACKET disjunction_expr RBRACKET lvalue_access?
|
||||||
|
-> ^(ARRAY_ACCESS disjunction_expr lvalue_access?)
|
||||||
|
;
|
||||||
|
|
||||||
|
funcall
|
||||||
|
: type_id LPAREN arg_list? RPAREN
|
||||||
|
-> ^(FUN_CALL type_id arg_list?)
|
||||||
|
;
|
||||||
|
|
||||||
|
expr_seq
|
||||||
|
: expr (SEMI expr)*
|
||||||
|
-> ^(EXPR_SEQ expr+)
|
||||||
|
| -> ^(EXPR_SEQ )
|
||||||
|
;
|
||||||
|
|
||||||
|
arg_list
|
||||||
|
: expr (COMMA expr)*
|
||||||
|
-> ^(ARGS_FIELDS expr+)
|
||||||
|
;
|
||||||
|
|
||||||
88
Grammar/tiger.tokens
Normal file
88
Grammar/tiger.tokens
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
ALIAS_DECL=4
|
||||||
|
AND=5
|
||||||
|
ARGS_FIELDS=6
|
||||||
|
ARRAYKEY=7
|
||||||
|
ARRAY_ACCESS=8
|
||||||
|
ARRAY_DECL=9
|
||||||
|
ARRAY_INST=10
|
||||||
|
ASCII_ESC=11
|
||||||
|
ASSIGN=12
|
||||||
|
BEGIN_COMMENT=13
|
||||||
|
BREAK=14
|
||||||
|
BREAKKEY=15
|
||||||
|
COLON=16
|
||||||
|
COMMA=17
|
||||||
|
COMMENTARY=18
|
||||||
|
DECL_BLOCK=19
|
||||||
|
DIGIT=20
|
||||||
|
DIV=21
|
||||||
|
DOKEY=22
|
||||||
|
DOT=23
|
||||||
|
ELSEKEY=24
|
||||||
|
ENDKEY=25
|
||||||
|
END_COMMENT=26
|
||||||
|
EQ=27
|
||||||
|
ESC_SEQ=28
|
||||||
|
EXPR_SEQ=29
|
||||||
|
FIELDS_INST=30
|
||||||
|
FIELD_ACCESS=31
|
||||||
|
FIELD_ACCESS_TERMINAL=32
|
||||||
|
FIELD_INST=33
|
||||||
|
FILL_IN_TYPE=34
|
||||||
|
FOR=35
|
||||||
|
FORKEY=36
|
||||||
|
FUNCTIONKEY=37
|
||||||
|
FUN_CALL=38
|
||||||
|
FUN_DECL=39
|
||||||
|
FUN_DECL_SEQ=40
|
||||||
|
FUN_TYPE_WRAPPER=41
|
||||||
|
GT=42
|
||||||
|
GTEQ=43
|
||||||
|
ID=44
|
||||||
|
IF=45
|
||||||
|
IFKEY=46
|
||||||
|
INKEY=47
|
||||||
|
INT=48
|
||||||
|
INTKEY=49
|
||||||
|
LBRACKET=50
|
||||||
|
LET=51
|
||||||
|
LETKEY=52
|
||||||
|
LETTER=53
|
||||||
|
LKEY=54
|
||||||
|
LPAREN=55
|
||||||
|
LT=56
|
||||||
|
LTEQ=57
|
||||||
|
MINUS=58
|
||||||
|
MULT=59
|
||||||
|
NEG=60
|
||||||
|
NIL=61
|
||||||
|
NILKEY=62
|
||||||
|
NOTEQ=63
|
||||||
|
OFKEY=64
|
||||||
|
OR=65
|
||||||
|
PARAM_DECL=66
|
||||||
|
PLUS=67
|
||||||
|
PRINTABLE_CHARACTER=68
|
||||||
|
PROGRAM=69
|
||||||
|
QUOTE=70
|
||||||
|
RBRACKET=71
|
||||||
|
RECORD_DECL=72
|
||||||
|
RECORD_INST=73
|
||||||
|
RKEY=74
|
||||||
|
RPAREN=75
|
||||||
|
SEMI=76
|
||||||
|
STRING=77
|
||||||
|
STRINGKEY=78
|
||||||
|
THENKEY=79
|
||||||
|
TOKEY=80
|
||||||
|
TYPE=81
|
||||||
|
TYPEKEY=82
|
||||||
|
TYPE_DECL=83
|
||||||
|
TYPE_DECL_SEQ=84
|
||||||
|
TYPE_FIELD=85
|
||||||
|
VARKEY=86
|
||||||
|
VAR_ACCESS=87
|
||||||
|
VAR_DECL=88
|
||||||
|
WHILE=89
|
||||||
|
WHILEKEY=90
|
||||||
|
WS=91
|
||||||
3313
Grammar/tigerLexer.cs
Normal file
3313
Grammar/tigerLexer.cs
Normal file
File diff suppressed because it is too large
Load Diff
6497
Grammar/tigerParser.cs
Normal file
6497
Grammar/tigerParser.cs
Normal file
File diff suppressed because it is too large
Load Diff
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2017 Juan Carlos Pujol Mainegra
|
Copyright (C) 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
502
README.md
Normal file
502
README.md
Normal file
@@ -0,0 +1,502 @@
|
|||||||
|
TL;DR
|
||||||
|
=====
|
||||||
|
|
||||||
|
- **YATC** (Yet Another Tiger Compiler) is a **Tiger** compiler that generates
|
||||||
|
**.Net framework** managed binaries.
|
||||||
|
|
||||||
|
- YATC was made in 2014 as part of an undergrad Computer Science project by
|
||||||
|
**Damian Valdés Santiago** and **Juan Carlos Pujol Mainegra**.
|
||||||
|
|
||||||
|
- **Tiger** is a relatively simple and imperative language that features
|
||||||
|
nested functions, records, arrays, integral variables and strings.
|
||||||
|
|
||||||
|
- Tiger was defined by **Andrew Appel** in 1998 for *Modern Compiler
|
||||||
|
Implementation in Java* (Cambridge University Press, 1998)
|
||||||
|
|
||||||
|
- This report will explain the architecture and implementation of a Tiger
|
||||||
|
compiler using **ANTLR 3.3.4** for the grammar definition, **C\# 5.0** in .Net
|
||||||
|
Framework 4.5 for the construction of the Abstract Syntax Tree (AST) and the
|
||||||
|
Dynamic Language Runtime (DLR) for the generation of the CIL/MSIL (Common
|
||||||
|
Intermediary Language/Microsoft Intermediary Language) code.
|
||||||
|
|
||||||
|
- **YATC** se presenta *tal cual* y no es mantenido de ninguna forma por sus
|
||||||
|
autores.
|
||||||
|
|
||||||
|
Resumen
|
||||||
|
-------
|
||||||
|
|
||||||
|
- **YATC** (Yet Another Tiger Compiler) es un compilador de **Tiger** que
|
||||||
|
genera binarios gestionados para **.Net framework**.
|
||||||
|
|
||||||
|
- YATC fue realizado en 2014 como parte de un proyecto de pregrado de Ciencias
|
||||||
|
de la Computación por **Damian Valdés Santiago** y **Juan Carlos Pujol
|
||||||
|
Mainegra**.
|
||||||
|
|
||||||
|
- **Tiger** es un lenguaje relativamente sencillo e imperativo con
|
||||||
|
funciones anidadas, records, arrays y variables enteras y de tipo cadena
|
||||||
|
*string*.
|
||||||
|
|
||||||
|
- Tiger fue definido por **Andrew Appel** en 1998 para *Modern Compiler
|
||||||
|
Implementation in Java* (Cambridge University Press, 1998)
|
||||||
|
|
||||||
|
- En este informe se explicará la arquitectura e implementación de un
|
||||||
|
compilador para el lenguaje Tiger usando **ANTLR 3.3.4** para la definición de
|
||||||
|
la gramática, **C\# 5.0** en .Net Framework 4.5 para la construcción del
|
||||||
|
*Árbol de Sintaxis Abstracta* (AST) y *Dynamic Language Runtime* (DLR) para la
|
||||||
|
generación de código CIL/MSIL (Common Intermediary Language/Microsoft
|
||||||
|
Intermediary Language).
|
||||||
|
|
||||||
|
- **YATC** se presenta *tal cual* y no es mantenido de ninguna forma por sus
|
||||||
|
autores.
|
||||||
|
|
||||||
|
License
|
||||||
|
=======
|
||||||
|
Yet Another Tiger Compiler (YATC)
|
||||||
|
|
||||||
|
Copyright (C) 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
|
||||||
|
Sobre el proceso de compilación
|
||||||
|
===============================
|
||||||
|
|
||||||
|
YATC se inscribe en el clásico proceso para compilar un programa,
|
||||||
|
dividiendo el mismo en cuatro fases en relación de dependencia:
|
||||||
|
|
||||||
|
- **Análisis Lexicográfico**: identifica subsecuencias de caracteres
|
||||||
|
significativas separando el flujo de entrada en estructuras lógicas
|
||||||
|
llamadas *tokens*. Tokens podrían ser identificadores, palabras
|
||||||
|
claves, operaciones, etc. del lenguaje que se desea implementar. La
|
||||||
|
componente de software que realiza esta operación se llama *lexer*.
|
||||||
|
En resumen, el lexer transforma una cadena de caracteres en una
|
||||||
|
secuencia de tokens.
|
||||||
|
|
||||||
|
- **Análisis sintáctico**: garantiza que la secuencia de tokens tenga
|
||||||
|
un sentido según las especificaciones del lenguaje, construyendo un
|
||||||
|
árbol de derivación (DT), que es una representación de las
|
||||||
|
secuencias de derivaciones de la gramática, para obtener un programa
|
||||||
|
en Tiger. La componente de software que realiza esta operación se
|
||||||
|
llama *parser* En resumen, el parser recibe como entrada una
|
||||||
|
secuencia de tokens y emite un AST como resultado del proceso.
|
||||||
|
|
||||||
|
- **Análisis o chequeo semántico**: verifica que en el lenguaje se
|
||||||
|
tengan sentencias con sentido obteniéndose un AST. En resumen, se
|
||||||
|
transforma de un DT a un AST.
|
||||||
|
|
||||||
|
- **Generación de código**: el AST se convierte en un árbol de
|
||||||
|
sintaxis concreta y se genera código para las instrucciones del
|
||||||
|
programa en cuestión.
|
||||||
|
|
||||||
|
####
|
||||||
|
|
||||||
|
Cuando se compila un programa en Tiger se intenta transformar el mismo
|
||||||
|
en una secuencia de tokens válida para Tiger, de lo contrario se produce
|
||||||
|
un error sintáctico. Luego, se construye el AST correspondiente según la
|
||||||
|
jerarquía de clases implementadas y el patrón *TreeAdaptor* de ANTLR que
|
||||||
|
conecta la fase anterior con la actual (chequeo semántico). Se verifica
|
||||||
|
que no existan problemas y se pasa a la generación de código. De lo
|
||||||
|
contrario se reporta error semántico y se detiene el proceso de
|
||||||
|
compilación.
|
||||||
|
|
||||||
|
Herramientas para la implementación
|
||||||
|
===================================
|
||||||
|
|
||||||
|
En la implementación de YATC se utilizaron los siguientes softwares y
|
||||||
|
lenguajes:
|
||||||
|
|
||||||
|
- ANTLR, ANTLWorks y JRE para la definición de la gramática de Tiger
|
||||||
|
(entiéndase reglas del lexer y del parser), su prueba y proceso de
|
||||||
|
*debug* para la detección de fallas.
|
||||||
|
|
||||||
|
- Lenguaje .NET C\# en Microsoft Visual Studio 2012 para la jerarquía
|
||||||
|
de nodos del AST, la realización del chequeo semántico y la
|
||||||
|
generación de código a través de DLR cuya elección se justificará
|
||||||
|
más adelante.
|
||||||
|
|
||||||
|
- PeVerify suministrado por el SDK de .Net para la corrección del
|
||||||
|
ensamblado.
|
||||||
|
|
||||||
|
- TigerTester 1.2 para la realización de pruebas.
|
||||||
|
|
||||||
|
Análisis lexicográfico y sintáctico
|
||||||
|
===================================
|
||||||
|
|
||||||
|
Comentarios anidados
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
Para lograr el anidamiento de los comentarios, primero se crearon las
|
||||||
|
reglas del lexer que se muestran a continuación continuación:
|
||||||
|
|
||||||
|
```antlr
|
||||||
|
fragment BEGIN_COMMENT : '/*';
|
||||||
|
fragment END_COMMENT : '*/' ;
|
||||||
|
COMMENTARY : BEGIN_COMMENT ( options {greedy=false;} : COMMENTARY | . )*
|
||||||
|
END_COMMENT {$channel = Hidden;};
|
||||||
|
```
|
||||||
|
|
||||||
|
Esta regla es engañosa: al probar un código con comentarios anidados
|
||||||
|
como el que se muestra no se produce error (lo cual es correcto), sin
|
||||||
|
embargo al crear un código con comentarios anidados y desbalanceados, no
|
||||||
|
se produce tampoco error y esto sí es incorrecto desde el punto de vista
|
||||||
|
sintáctico.
|
||||||
|
|
||||||
|
```antlr
|
||||||
|
/* Comment1 /* Comment2 */ Comment3 */
|
||||||
|
```
|
||||||
|
|
||||||
|
El comportamiento por defecto de ANTLR al intentar realizar *match* a
|
||||||
|
una secuencia es consumirla entera. En ocasiones (como la que se
|
||||||
|
discute) esto no se desea. Este propósito se logra con *options
|
||||||
|
{greedy=false;}*. El operador *wildcard* ($\cdot$) representa a
|
||||||
|
cualquier caracter y por tanto, aunque se coloque en la regla `options
|
||||||
|
{greedy=false;} : COMMENTARY | $\cdot$`, ANTLR no es capaz de
|
||||||
|
diferenciar que el `COMMENTARY` no debe estar incluido en lo reconocido
|
||||||
|
por el operador *wildcard*.
|
||||||
|
|
||||||
|
Luego de notar estos errores se redefinen las reglas, obligando a que se
|
||||||
|
consuman caracteres hasta encontrar un bloque de comentarios seguido de
|
||||||
|
cualquier secuencia de caracteres.
|
||||||
|
|
||||||
|
```antlr
|
||||||
|
COMMENTARY : BEGIN_COMMENT ( options {greedy=false;} : . )*
|
||||||
|
(COMMENTARY ( options {greedy=false;} : . )* )*
|
||||||
|
END_COMMENT {$channel = Hidden;}
|
||||||
|
```
|
||||||
|
|
||||||
|
Los programa vacíos también son programas
|
||||||
|
-----------------------------------------
|
||||||
|
|
||||||
|
Un programa en Tiger es una expresión. Un programa que no tenga código
|
||||||
|
escrito también debe reconocerse y no debe presentar ningún tipo de
|
||||||
|
error. Esto se logra permitiendo que la regla principal, que produce una
|
||||||
|
expresión, permita que esta no esté a través del operador el operador
|
||||||
|
$?$.
|
||||||
|
|
||||||
|
|
||||||
|
```antlr
|
||||||
|
public a_program : expr ? EOF;
|
||||||
|
```
|
||||||
|
|
||||||
|
`a = b = c` es ilegal
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
Sea la siguiente regla del parser:
|
||||||
|
|
||||||
|
```antlr
|
||||||
|
relational_expr : arith_expr ((EQ | NOTEQ | GT | LT | GTEQ | LTEQ ) arith_expr)?;
|
||||||
|
```
|
||||||
|
|
||||||
|
El operador `?` al final de la regla le da la no asociatividad requerida
|
||||||
|
a los operadores de comparación (notar que en `arith_expr` y
|
||||||
|
`term_expr` se usa `*` para anidar estas expresiones en una lista). Con
|
||||||
|
esta regla no se permite `a = b = c`, pero si `a = (b = c)` como está
|
||||||
|
especificado.
|
||||||
|
|
||||||
|
Ayudando al chequeo semántico del for
|
||||||
|
-------------------------------------
|
||||||
|
|
||||||
|
Según las especificaciones del lenguaje Tiger, en un for solo se pueden
|
||||||
|
evaluar condiciones que retornen valor, por ello se exige en la regla
|
||||||
|
correspondiente que la primera y segunda expresiones sean
|
||||||
|
`disjunction_expr`.
|
||||||
|
|
||||||
|
```antlr
|
||||||
|
for_expr : FORKEY ID ASSIGN disjunction_expr TOKEY disjunction_expr DOKEY expr;
|
||||||
|
```
|
||||||
|
|
||||||
|
Reescritura de reglas
|
||||||
|
---------------------
|
||||||
|
|
||||||
|
La mayoría de las reglas del parser se reescriben creando nodos
|
||||||
|
“ficticios” en la sección tokens que se usarán para generar nodos
|
||||||
|
homólogos del AST a través del TreeAdaptor. Un ejemplo representativo es
|
||||||
|
la creación de un token para el operador de negación (menos unario).
|
||||||
|
Esto permite además eliminar del DT del programa, tokens innecesarios
|
||||||
|
para la próxima fase, como los paréntesis, las palabras claves, las
|
||||||
|
comas, los punto y coma, etc.
|
||||||
|
|
||||||
|
Análisis semántico
|
||||||
|
==================
|
||||||
|
|
||||||
|
Jerarquía de nodos del AST
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
Para que el TreeAdaptor funcione correctamente, se crean las clases
|
||||||
|
correspondientes a los nodos que desean comprobarse semánticamente. Cada
|
||||||
|
nodo tiene, además de sus propiedades estructurales, dos métodos
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
public abstract void CheckSemantics(TigerScope scope, Report report);
|
||||||
|
internal abstract void GenerateCode(ModuleBuilder moduleBuilder);
|
||||||
|
```
|
||||||
|
|
||||||
|
El primero (fundamental en esta fase) chequea la semántica del nodo en
|
||||||
|
cuestión, siguiendo un patrón de gramática atributada: un nodo está bien
|
||||||
|
si sus hijos lo están y la información brindada por sus hijos o hermanos
|
||||||
|
en la jerarquía no produce error en su funcionamiento. Estas
|
||||||
|
informaciones se modelan a través de las clases herederas de
|
||||||
|
`TigerInfo`, que dejan información conectada para la generación de
|
||||||
|
código, p.e *bindings* de variables, información relativa a la funciones
|
||||||
|
(nombre, tipo y cantidad de parámetros), etc.
|
||||||
|
|
||||||
|
Notar que el nodo `UnknownNode` se usa para homologar tokens que, si
|
||||||
|
bien no introducen error en el chequeo semántico, ayudan en la
|
||||||
|
comprensión y organización del AST. Ejemplos de token que se mapean como
|
||||||
|
UnknownNode son `FIELDS_INST`, `PARAM_DECL`, `FUN_TYPE_WRAPPER`,
|
||||||
|
`DECL_BLOCK` y `ARGS_FIELDS`.
|
||||||
|
|
||||||
|
Scope
|
||||||
|
-----
|
||||||
|
|
||||||
|
Al usar una variable, un `type` o una función, ¿cómo se sabe si fueron
|
||||||
|
declaradas antes? Esta información se obtiene de la clase `TigerScope`
|
||||||
|
que almacena nombres, con sus respectivos objetos, al que se refieren
|
||||||
|
(variables, *types* o funciones) como una tabla de símbolos. Además,
|
||||||
|
esta clase permite, mediante un árbol de scopes, anidar funciones y
|
||||||
|
expresiones `let`, de forma que solo pueda tenerse acceso a las
|
||||||
|
variables o funciones que están declaradas en ese ámbito o en el ámbito
|
||||||
|
del nodo padre. Esta clase además se encarga del binding de las
|
||||||
|
variables. Esta estructura permite que, en caso de que en un scope
|
||||||
|
interno se cree un `type` o variable de igual nombre que un `type` o
|
||||||
|
variable de un scope ancestro del actual, entonces se reescriba el valor
|
||||||
|
antiguo del `type` o variable por el nuevo y se lanzará una advertencia.
|
||||||
|
|
||||||
|
Nil
|
||||||
|
---
|
||||||
|
|
||||||
|
En el chequeo semántico de la expresión `if cond_expr then then_expr
|
||||||
|
else else_expr` se realizan las siguientes comprobaciones. Se realiza
|
||||||
|
el chequeo semántico de `cond_expr` y luego el de `then_expr`. Si
|
||||||
|
ambas expresiones están correctas se comprueba que la `cond_expr` tenga
|
||||||
|
algún valor (no sea `void`) y luego se comprueba que este valor sea
|
||||||
|
entero. De lo contrario se lanzan errores. En el caso que exista parte
|
||||||
|
`else` el chequeo es más arduo. Primero se chequea que el nodo
|
||||||
|
`else_expr` esté correcto. Luego se verifica que el tipo de
|
||||||
|
`else_expr` sea asignable, o sea, `int`, `string`, `record`, `array`.
|
||||||
|
De lo contrario, se reporta error. En este punto es donde se chequean
|
||||||
|
los casos críticos del `nil` expuestos en las especificaciones de Tiger.
|
||||||
|
|
||||||
|
Secuencia de declaraciones de tipo
|
||||||
|
----------------------------------
|
||||||
|
|
||||||
|
La construcción y verificación de las secuencias de declaraciones de
|
||||||
|
tipos dentro de un `let` se realiza mediante tres pasadas a cada uno de
|
||||||
|
los nodos hijos de cada una de ellas, dígase a los `TypeDeclNode` que
|
||||||
|
están contenidos dentro de cada `TypeDeclSeqNode`, encargado de agrupar
|
||||||
|
las mismas. Esto se consigue mediante la regla del parser
|
||||||
|
|
||||||
|
```antlr
|
||||||
|
decl : type_decl+ | var_decl | fun_decl+;
|
||||||
|
```
|
||||||
|
|
||||||
|
Esta regla está contenida dentro de un iterador + **EBNF** (*Extended
|
||||||
|
Backus-Naur Form*), leyéndose finalmente
|
||||||
|
|
||||||
|
```antlr
|
||||||
|
let_expr : LETKEY decl+ INKEY expr_seq ENDKEY;
|
||||||
|
```
|
||||||
|
|
||||||
|
Aunque esta última pareja constituya una ambigüedad, se usa a propósito
|
||||||
|
para realizar dicha agrupación, de forma análoga a la selección de a qué
|
||||||
|
`if` pertenece un determinado `else`, esto es, al de mayor anidamiento.
|
||||||
|
Esto facilita sobremanera cualquier operación posterior que se realice
|
||||||
|
sobre los conjuntos de declaraciones, ya sean de tipos o de funciones.
|
||||||
|
Las variables se tratan como conjuntos *singleton*.
|
||||||
|
|
||||||
|
Dada una declaración de tipo
|
||||||
|
|
||||||
|
```antlr
|
||||||
|
type_decl : TYPEKEY ID EQ type;
|
||||||
|
```
|
||||||
|
|
||||||
|
se le dice parte izquierda o cabeza al token ID, que corresponde al
|
||||||
|
nombre del tipo que se quiere declarar. La parte derecha corresponde a
|
||||||
|
la regla `type`, que puede ser `type_id`, `array_decl` o
|
||||||
|
`record_decl`.
|
||||||
|
|
||||||
|
Los pasos son los siguientes:
|
||||||
|
|
||||||
|
1. Se agregan las cabezas de los tipos, es decir, la parte izquierda de
|
||||||
|
la declaración de cada uno de ellos, con el sólo propósito de comprobar
|
||||||
|
que no exista una redeclaración dentro de una secuencia, o de forma
|
||||||
|
local y global, agregándolo a una estructura que contiene cada
|
||||||
|
`TypeDeclNode` como llave del nombre del tipo. Este paso incluye
|
||||||
|
detectar renombramientos de los nombres de los tipos primitivos como
|
||||||
|
tipos de usuario, esto es, no cambiar a `int` o `string`, por ejemplo.
|
||||||
|
En este paso se llama al `CheckHeader(TigerScope scope, Report report,
|
||||||
|
string name)` de cada hijo.
|
||||||
|
|
||||||
|
2. Para detectar eventuales ciclos es necesario construir un grafo
|
||||||
|
dirigido $G \langle V,E \rangle$ con
|
||||||
|
|
||||||
|
$$V = \{ \text{nodos de declaración de tipos} \}$$
|
||||||
|
|
||||||
|
$$E = \{ (u,v) | ParteIzquierda(u) \and BaseType(u, v) \in { Alias V Array } \and ParteDerecha(v), u, v \in V \}$$
|
||||||
|
|
||||||
|
Dicha construcción asegura que las aristas se establezcan sólo entre
|
||||||
|
nodos de declaraciones de tipos que estén conectados como parte
|
||||||
|
izquierda y derecha del *statement*, que sea de base `Array` o `Alias`, es
|
||||||
|
decir, un tipo cuyo tipo, valga la redundancia, sea alguno de los
|
||||||
|
anteriores. Estos se agregan a una estructura
|
||||||
|
`Dictionary<TypeDeclNode, Node<TypeDeclNode>>`
|
||||||
|
donde `Node<T>` es una representación de nodos en un
|
||||||
|
grafo, con soporte para realizar operaciones de orden topológico además
|
||||||
|
de detectar ciclos y su composición. Esta estructura permite conectar
|
||||||
|
los `TypeDeclNode` en el grafo con aquellos otros de la secuencia que se
|
||||||
|
requiera, y evitar repetir nodos en el grafo que correspondan a una
|
||||||
|
misma declaración. Esto podría suceder dado que un mismo tipo puede
|
||||||
|
estar varias veces en parte derecha, y a lo sumo una vez en parte
|
||||||
|
izquierda si se ha llegado a este paso (el anterior elimina que esté más
|
||||||
|
de una vez). De esta forma no se tenderá una arista entre un tipo
|
||||||
|
`aliasInt` que sea alias de `int`, dado que la parte derecha no estará
|
||||||
|
en primer lugar en la secuencia, y por tanto no puede referenciar hacia
|
||||||
|
adelante, de forma que no hay peligro de recursión cíclica.
|
||||||
|
|
||||||
|
3. Al grafo construido se le realiza un DFS (*Depth-First Search*) con
|
||||||
|
marcado de nodos, en los que se detecta una arista back-edge, o de
|
||||||
|
retroceso, si se en la navegación se llega a un nodo marcado como gris
|
||||||
|
\[ver *Intro To Algorithms*, Cormen et al., alrededor de la 614\]. La prueba
|
||||||
|
del Teorema 22.12 acompaña el resultado. Si existe un ciclo se detecta
|
||||||
|
sus nodos componentes y se imprime. De no existir, se recorre el orden
|
||||||
|
topológico de los nodos resultantes, cuyo valor contenido son las
|
||||||
|
declaraciones de nodos, y se llama a `CheckSemantics` de cada uno en
|
||||||
|
dicho orden. La generación de código también se realiza por dicho orden.
|
||||||
|
Esto evita tener que realizar múltiples búsquedas si para construir
|
||||||
|
finalmente un tipo, aunque se conozca que está correcto.
|
||||||
|
|
||||||
|
####
|
||||||
|
|
||||||
|
El último paso, detectar un ciclo, y después navegar por el orden
|
||||||
|
topológico dado, devuelto en forma de lista enlazada, es vital para la
|
||||||
|
realización de las operaciones de construcción de tipos, que se realizan
|
||||||
|
de forma transparente para los hijos, los cuales no requieren búsquedas
|
||||||
|
adicionales para la determinación completa de los mismos, como se había
|
||||||
|
dicho. Para ellos se habilita que los nodos que así lo requieran no
|
||||||
|
contengan una referencia directa al tipo, dado que en el momento de
|
||||||
|
declaración y comprobación semántica, aunque este sea correcto, puede no
|
||||||
|
estar determinado, sencillamente porque es el tipo de un record. Uno de
|
||||||
|
estos casos se analiza a continuación.
|
||||||
|
|
||||||
|
####
|
||||||
|
|
||||||
|
Supongamos que deseamos comprobar y construir los tipos del siguiente
|
||||||
|
programa:
|
||||||
|
|
||||||
|
```antlr
|
||||||
|
let
|
||||||
|
type a = b
|
||||||
|
type b = { x: c }
|
||||||
|
type c = a
|
||||||
|
in
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
El programa está correcto, dado que los tipos `a`, `b` y `c` están bien
|
||||||
|
determinados por un ciclo que pasa por al menos un tipo record (`b`). El
|
||||||
|
tipo básico de `a` es el mismo que el de `c`, que es alias. El orden
|
||||||
|
topológico podrá arrojar una sola forma de navegarlo para la
|
||||||
|
construcción. Sea esta $b \rightarrow a \rightarrow c$. Al comprobar
|
||||||
|
semánticamente a `b` el tipo no está bien determinado, dado que tiene un
|
||||||
|
campo `x` de un tipo desconocido `c`, pero se conoce que la declaración
|
||||||
|
esta correcta, dado que `c` existe y está correcto, por lo que `b` está
|
||||||
|
bien determinado. Su definición, no obstante, no es completa. Por lo
|
||||||
|
tanto se almacena una referencia a la referencia a la definición del
|
||||||
|
mismo, de forma que esta pueda se alterada, es decir se puede completar,
|
||||||
|
y todos los nodos que dependían de esta queden bien.
|
||||||
|
|
||||||
|
####
|
||||||
|
|
||||||
|
Finalmente los tipos alias no se analiza semánticamente, es decir, son
|
||||||
|
indistinguibles las definiciones originales de `a`, `b` y `c`, a efectos
|
||||||
|
del resto del programa.
|
||||||
|
|
||||||
|
Generación de código
|
||||||
|
====================
|
||||||
|
|
||||||
|
La generación de código es el proceso mediante el cual un compilador
|
||||||
|
convierte la información proveniente de las fases anteriores en código
|
||||||
|
que puede ser ejecutado por una computadora. La ejecución del código
|
||||||
|
generado la realiza, en este compilador, el *Common Language Runtime*
|
||||||
|
(CLR) de la plataforma .NET. El CLR usa una estructura basada en un
|
||||||
|
modelo de pila para modelar la ejecución de código, así como un lenguaje
|
||||||
|
ensamblador propio llamado *Intermediate Language* (IL). Nuestro
|
||||||
|
compilador utiliza una componente de la biblioteca de clase de la
|
||||||
|
plataforma .NET llamada `System.Reflection.Emit` que permite crear un
|
||||||
|
ejecutable en .NET y agregarle código según se desee; y para generar
|
||||||
|
código IL se usa *Dynamic Language Runtime* (DLR).
|
||||||
|
|
||||||
|
¿Por qué usar DLR?
|
||||||
|
------------------
|
||||||
|
|
||||||
|
El DLR proporciona servicios para implementar lenguajes dinámicos sobre
|
||||||
|
el CLR. DLR facilita el desarrollo de lenguajes para ejecutar programas
|
||||||
|
sobre .NET Framework. Los lenguajes por el tipo del objeto que los
|
||||||
|
representa en tiempo de ejecución, que se especifica en tiempo de diseño
|
||||||
|
en un lenguaje estáticamnete tipado como C\#.
|
||||||
|
|
||||||
|
DLR permite a los programadores implementar lexers, parsers,
|
||||||
|
analizadores semánticos y generadores de código y la generación de
|
||||||
|
código se realiza de forma declarativa mediante árboles de expresiones
|
||||||
|
lo que hace más fácil esta etapa.
|
||||||
|
|
||||||
|
Funciones recursivas
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
En la generación de código de las secuencias de funciones se realizan
|
||||||
|
tres etapas.
|
||||||
|
|
||||||
|
1. Se recorren todos las definiciones (`headers`) de las funciones en
|
||||||
|
la secuencia y se introducen en una lista de `ParameterExpression`
|
||||||
|
que después se le pasará a la segunda fase.
|
||||||
|
|
||||||
|
2. Por cada llamado a la propia función (recursión) o a otra, se crea
|
||||||
|
una `Expression` ficticia en el que luego se sustituirá el código
|
||||||
|
del llamado a la función.
|
||||||
|
|
||||||
|
3. Se recorren las declaraciones de funciones en la secuencia y se le
|
||||||
|
asigna a la `Expression` ficticia el código del llamado a la función
|
||||||
|
correspondiente.
|
||||||
|
|
||||||
|
Para más información
|
||||||
|
====================
|
||||||
|
|
||||||
|
- Edwards, Stephen - *Tiger Language Reference Manual*, Columbia
|
||||||
|
University.
|
||||||
|
|
||||||
|
- Appel, Andrew and Ginsburg, Maia - *Modern Compiler Implementation
|
||||||
|
in C*, Press Syndicate of the University of Cambridge, 1998.
|
||||||
|
|
||||||
|
- Parr, Terrence - *The Definitive ANTLR Reference*, The Pragmatic
|
||||||
|
Programmers, May 2007.
|
||||||
|
|
||||||
|
- Parr, Terrence - *The Definitive ANTLR 4 Reference*, The Pragmatic
|
||||||
|
Programmers, Jan 2013.
|
||||||
|
|
||||||
|
- Parr, Terrence - *Language Implementation Patterns*, The Pragmatic
|
||||||
|
Programmers, 2010.
|
||||||
|
|
||||||
|
- Wu, Chaur - *Pro DLR in .NET 4*, Apress, 2010.
|
||||||
|
|
||||||
|
- Chiles, Bill - *Expression Trees v2 Spec* (DRAFT).
|
||||||
|
|
||||||
|
- Cormen, Thomas *et al.* - *Introduction to algorithms*, 3rd ed,
|
||||||
|
Massachusetts Institute of Technology, 2009.
|
||||||
|
|
||||||
|
- [https://docs.microsoft.com/](https://docs.microsoft.com/ "MSDN")
|
||||||
|
|
||||||
6
YATC.Compiler/App.config
Normal file
6
YATC.Compiler/App.config
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<configuration>
|
||||||
|
<startup>
|
||||||
|
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
|
||||||
|
</startup>
|
||||||
|
</configuration>
|
||||||
253
YATC.Compiler/Program.cs
Normal file
253
YATC.Compiler/Program.cs
Normal file
@@ -0,0 +1,253 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using Antlr.Runtime.Tree;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.ASTNodes;
|
||||||
|
using YATC.Grammar;
|
||||||
|
|
||||||
|
namespace YATC.Compiler
|
||||||
|
{
|
||||||
|
static class Program
|
||||||
|
{
|
||||||
|
enum ErrorKind { Fatal, Syntax, Semantic }
|
||||||
|
|
||||||
|
public static readonly string FileName = Assembly.GetExecutingAssembly().GetName().Name;
|
||||||
|
public const string Name = "Yet Another Tiger Compiler";
|
||||||
|
public const string Copyright = "Copyright D. Valdes Santiago & Juan C. Pujol Mainegra (c) 2013-2014";
|
||||||
|
public const string ShortName = "YATC";
|
||||||
|
|
||||||
|
private static TextWriterTraceListener _textListener = null;
|
||||||
|
private const string MessageFormatHead = "({0}, {1}): ";
|
||||||
|
private const string MessageFormatBody = "{0} {1}: {2}";
|
||||||
|
|
||||||
|
private const string Usage =
|
||||||
|
"Usage :" +
|
||||||
|
"\ttiger.exe path_to_program_file.tig"
|
||||||
|
;
|
||||||
|
|
||||||
|
static void Write(int line, int column, ErrorKind errorKind, Level level, string message, params object[] objects)
|
||||||
|
{
|
||||||
|
string msg = string.Format(message, objects);
|
||||||
|
string wrappedMsg = string.Format(MessageFormatHead, line, column);
|
||||||
|
Trace.Write(wrappedMsg);
|
||||||
|
|
||||||
|
ConsoleColor lastColor = Console.ForegroundColor;
|
||||||
|
switch (level)
|
||||||
|
{
|
||||||
|
case Level.Info:
|
||||||
|
Console.ForegroundColor = ConsoleColor.Gray;
|
||||||
|
break;
|
||||||
|
case Level.Warning:
|
||||||
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
break;
|
||||||
|
case Level.Error:
|
||||||
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
wrappedMsg = string.Format(MessageFormatBody, errorKind, level, msg);
|
||||||
|
Trace.WriteLine(wrappedMsg);
|
||||||
|
|
||||||
|
Console.ForegroundColor = lastColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PrintUsage()
|
||||||
|
{
|
||||||
|
Trace.WriteLine(Usage);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PrintWelcome()
|
||||||
|
{
|
||||||
|
Trace.WriteLine(Name + " version " + Assembly.GetExecutingAssembly().GetName().Version + '\n' + Copyright);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PrintBlank()
|
||||||
|
{
|
||||||
|
Trace.WriteLine(string.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Flush()
|
||||||
|
{
|
||||||
|
Trace.Flush();
|
||||||
|
Debug.Flush();
|
||||||
|
if (_textListener != null)
|
||||||
|
{
|
||||||
|
_textListener.Flush();
|
||||||
|
_textListener.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ExitBad(string programName)
|
||||||
|
{
|
||||||
|
Flush();
|
||||||
|
Write(0, 0, ErrorKind.Fatal, Level.Error,
|
||||||
|
"Could not generate executable for program '{0}': One or more errors were found.",
|
||||||
|
programName);
|
||||||
|
PrintBlank();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int Main(string[] args)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_textListener = new TextWriterTraceListener(FileName + ".log");
|
||||||
|
Trace.Listeners.Add(_textListener);
|
||||||
|
|
||||||
|
_textListener.WriteLine(string.Empty.PadLeft(79, '='));
|
||||||
|
_textListener.WriteLine(string.Format("Tracing log for program '{0}' on '{1}'.",
|
||||||
|
args.Length >= 1 ? args[0] : "not specified", DateTime.Now.ToString(CultureInfo.InvariantCulture)));
|
||||||
|
#if DEBUG
|
||||||
|
_textListener.WriteLine("Debug mode is ON.");
|
||||||
|
#else
|
||||||
|
_textListener.WriteLine("Debug mode is OFF.");
|
||||||
|
#endif
|
||||||
|
_textListener.WriteLine(string.Empty.PadLeft(79, '='));
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
Trace.Listeners.Add(new ConsoleTraceListener());
|
||||||
|
|
||||||
|
PrintWelcome();
|
||||||
|
PrintBlank();
|
||||||
|
|
||||||
|
if (args.Length != 1)
|
||||||
|
{
|
||||||
|
Write(0, 0, ErrorKind.Fatal, Level.Error,
|
||||||
|
"Wrong number of argument: expecting one and {0} found.",
|
||||||
|
args.Length);
|
||||||
|
return ExitBad(string.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
ProgramNode programNode;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ICharStream charStream = new ANTLRFileStream(args[0]);
|
||||||
|
var lexer = new tigerLexer(charStream);
|
||||||
|
var tokenStream = new CommonTokenStream(lexer);
|
||||||
|
var parser = new tigerParser(tokenStream)
|
||||||
|
{
|
||||||
|
TraceDestination = Console.Out,
|
||||||
|
TreeAdaptor = new TigerTreeAdaptor()
|
||||||
|
};
|
||||||
|
|
||||||
|
programNode = parser.a_program().Tree as ProgramNode;
|
||||||
|
}
|
||||||
|
catch (ParsingException e)
|
||||||
|
{
|
||||||
|
Write(e.RecognitionError.Line, e.RecognitionError.CharPositionInLine,
|
||||||
|
ErrorKind.Syntax, Level.Error, "Parsing input file: '{0}'", e.Message);
|
||||||
|
return ExitBad(args[0]);
|
||||||
|
}
|
||||||
|
catch (DirectoryNotFoundException)
|
||||||
|
{
|
||||||
|
Write(0, 0,
|
||||||
|
ErrorKind.Fatal, Level.Error, "Directory '{0}' could not be found.",
|
||||||
|
Path.GetDirectoryName(args[0]));
|
||||||
|
return ExitBad(args[0]);
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException fileNotFound)
|
||||||
|
{
|
||||||
|
Write(0, 0,
|
||||||
|
ErrorKind.Fatal, Level.Error, "File '{0}' could not be found.",
|
||||||
|
fileNotFound.FileName);
|
||||||
|
return ExitBad(args[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (programNode == null)
|
||||||
|
return ExitBad(args[0]);
|
||||||
|
|
||||||
|
Print(programNode);
|
||||||
|
programNode.CheckSemantics();
|
||||||
|
|
||||||
|
foreach (var item in programNode.Report)
|
||||||
|
{
|
||||||
|
Write(item.Line, item.Column, ErrorKind.Semantic, item.Level, item.Text);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (programNode.Report.Level != Level.Error)
|
||||||
|
{
|
||||||
|
string name = Path.HasExtension(args[0]) ?
|
||||||
|
Path.GetFileNameWithoutExtension(args[0]) :
|
||||||
|
args[0];
|
||||||
|
|
||||||
|
string fileName = Path.HasExtension(args[0])
|
||||||
|
? Path.ChangeExtension(Path.GetFileName(args[0]), "exe")
|
||||||
|
: args[0] + ".exe";
|
||||||
|
|
||||||
|
string directory = Path.GetDirectoryName(args[0]);
|
||||||
|
|
||||||
|
AssemblyBuilder programAssembly = programNode.GenerateCode(
|
||||||
|
name,
|
||||||
|
fileName,
|
||||||
|
directory == string.Empty ? null : directory);
|
||||||
|
programAssembly.Save(fileName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return ExitBad(args[0]);
|
||||||
|
|
||||||
|
Flush();
|
||||||
|
PrintBlank();
|
||||||
|
return programNode.Report.Level != Level.Error ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Print(CommonTree tree)
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
Debug.IndentSize = 4;
|
||||||
|
foreach (var child in PrintTree(tree))
|
||||||
|
{
|
||||||
|
Debug.IndentLevel = child.Item2;
|
||||||
|
Debug.WriteLine(
|
||||||
|
/*(child.Item1.ChildCount != 0 ? "+ " : " ") +*/
|
||||||
|
child.Item1.Text + "\t" + child.Item1.GetType()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Debug.IndentLevel = 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static IEnumerable<Tuple<BaseTree, int>> PrintTree(BaseTree tree, int level = 0)
|
||||||
|
{
|
||||||
|
yield return new Tuple<BaseTree, int>(tree, level);
|
||||||
|
if (tree.Children == null)
|
||||||
|
yield break;
|
||||||
|
|
||||||
|
foreach (var child in tree.Children.Cast<BaseTree>())
|
||||||
|
foreach (var grandChild in PrintTree(child, level + 1))
|
||||||
|
yield return grandChild;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
YATC.Compiler/Properties/AssemblyInfo.cs
Normal file
36
YATC.Compiler/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// General Information about an assembly is controlled through the following
|
||||||
|
// set of attributes. Change these attribute values to modify the information
|
||||||
|
// associated with an assembly.
|
||||||
|
[assembly: AssemblyTitle("YATC")]
|
||||||
|
[assembly: AssemblyDescription("Yet Another Tiger Compiler")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("")]
|
||||||
|
[assembly: AssemblyProduct("YATC")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright Damian Valdes Santiago & Juan Carlos Pujol Mainegra © 2013-2014")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
// Setting ComVisible to false makes the types in this assembly not visible
|
||||||
|
// to COM components. If you need to access a type in this assembly from
|
||||||
|
// COM, set the ComVisible attribute to true on that type.
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||||
|
[assembly: Guid("f8cf7ad4-d691-4fc6-8302-a2216c6a8b77")]
|
||||||
|
|
||||||
|
// Version information for an assembly consists of the following four values:
|
||||||
|
//
|
||||||
|
// Major Version
|
||||||
|
// Minor Version
|
||||||
|
// Build Number
|
||||||
|
// Revision
|
||||||
|
//
|
||||||
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
|
// by using the '*' as shown below:
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
[assembly: AssemblyVersion("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||||
67
YATC.Compiler/Tiger.csproj
Normal file
67
YATC.Compiler/Tiger.csproj
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProjectGuid>{8B1FA297-B497-4F06-A6AE-87D73920AA57}</ProjectGuid>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>YATC.Compiler</RootNamespace>
|
||||||
|
<AssemblyName>tiger</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="Antlr3.Runtime">
|
||||||
|
<HintPath>..\Antlr3.Runtime.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="Program.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="App.config" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\YATC\YATC.csproj">
|
||||||
|
<Project>{d62da087-4724-49a3-91b5-83689f442747}</Project>
|
||||||
|
<Name>YATC</Name>
|
||||||
|
</ProjectReference>
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
<Target Name="BeforeBuild">
|
||||||
|
</Target>
|
||||||
|
<Target Name="AfterBuild">
|
||||||
|
</Target>
|
||||||
|
-->
|
||||||
|
</Project>
|
||||||
1
YATC.Compiler/Tiger.idc
Normal file
1
YATC.Compiler/Tiger.idc
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<Configurations active="Default"><Configuration name="Default"><GuestCommandRemote val="project command through a shared folder"></GuestCommandRemote><ShareFoldersRemote val=""></ShareFoldersRemote><RemoteDebugMonitor val="C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Remote Debugger\x86\msvsmon.exe"></RemoteDebugMonitor><MonitorName val="VMDebug"></MonitorName><RemoteVM val=""></RemoteVM><StartMode val="No"></StartMode><TerminationModeRemote val="No operation"></TerminationModeRemote><CopyFilesRemote val=""></CopyFilesRemote><PreRemoteCommandLine val=""></PreRemoteCommandLine><PostRemoteCommandLine val=""></PostRemoteCommandLine><RecordingToReplay val=""></RecordingToReplay><ReplayVM val=""></ReplayVM><BaseSnapshotForRecording val=""></BaseSnapshotForRecording><CopyFilesRecord val=""></CopyFilesRecord><PreRecordCommandLine val=""></PreRecordCommandLine><PostRecordCommandLine val=""></PostRecordCommandLine><TerminationModeRecord val="No operation"></TerminationModeRecord><InstanceToDebug val=""></InstanceToDebug><GuestCommandReplay val="project command through a shared folder"></GuestCommandReplay><ShareFoldersRecord val=""></ShareFoldersRecord><RemoteReplayFlag val=""></RemoteReplayFlag><RemoteMachine val=""></RemoteMachine><RemoteReplayVM val=""></RemoteReplayVM><RemoteRecordingToReplay val=""></RemoteRecordingToReplay><RemoteReplayPasscode val=""></RemoteReplayPasscode><HostSearchPath val=""></HostSearchPath></Configuration></Configurations>
|
||||||
26
YATC.sln
Normal file
26
YATC.sln
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio 2012
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YATC", "YATC\YATC.csproj", "{D62DA087-4724-49A3-91B5-83689F442747}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tiger", "YATC.Compiler\Tiger.csproj", "{8B1FA297-B497-4F06-A6AE-87D73920AA57}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{D62DA087-4724-49A3-91B5-83689F442747}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{D62DA087-4724-49A3-91B5-83689F442747}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{D62DA087-4724-49A3-91B5-83689F442747}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{D62DA087-4724-49A3-91B5-83689F442747}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{8B1FA297-B497-4F06-A6AE-87D73920AA57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{8B1FA297-B497-4F06-A6AE-87D73920AA57}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{8B1FA297-B497-4F06-A6AE-87D73920AA57}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{8B1FA297-B497-4F06-A6AE-87D73920AA57}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
69
YATC/ASTNodes/LocalNode/DeclarationNode/AliasDeclNode.cs
Normal file
69
YATC/ASTNodes/LocalNode/DeclarationNode/AliasDeclNode.cs
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class AliasDeclNode : DeclarationNode
|
||||||
|
{
|
||||||
|
public AliasDeclNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeNode TypeNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||||
|
|
||||||
|
public override bool CheckHeader(TigerScope scope, Report report, string name)
|
||||||
|
{
|
||||||
|
this.TigerTypeInfo = new TigerTypeInfo(name, new TigerTypeHolder(), false);
|
||||||
|
scope.Add(this.TigerTypeInfo);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
// type is an alias of another type, then follow.
|
||||||
|
TigerTypeInfo aliasTo = scope.FindTypeInfo(this.TypeNode.Name, false);
|
||||||
|
|
||||||
|
if (aliasTo == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Alias to undeclared type: '{0}'.", this.TypeNode.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerTypeInfo.Holder.TigerType = aliasTo.Holder.TigerType;
|
||||||
|
this.IsOK = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
70
YATC/ASTNodes/LocalNode/DeclarationNode/ArrayDeclNode.cs
Normal file
70
YATC/ASTNodes/LocalNode/DeclarationNode/ArrayDeclNode.cs
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
internal class ArrayDeclNode: DeclarationNode
|
||||||
|
{
|
||||||
|
public ArrayDeclNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private ArrayType _arrayType;
|
||||||
|
public TypeNode ElementTypeNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||||
|
|
||||||
|
public override bool CheckHeader(TigerScope scope, Report report, string name)
|
||||||
|
{
|
||||||
|
this.TigerTypeInfo = new TigerTypeInfo(name, new TigerTypeHolder(), false);
|
||||||
|
scope.Add(this.TigerTypeInfo);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
TigerTypeInfo elementTigerTypeInfo = scope.FindTypeInfo(this.ElementTypeNode.Name, false);
|
||||||
|
|
||||||
|
if (elementTigerTypeInfo == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Undeclared type: array element '{0}'.", this.ElementTypeNode.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
this.TigerTypeInfo.Holder.TigerType = _arrayType = new ArrayType(this.TigerTypeInfo.Name, elementTigerTypeInfo.Holder);
|
||||||
|
this.IsOK = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
_arrayType.CLRType = _arrayType.ElementTypeHolder.TigerType.GetCLRType().MakeArrayType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
50
YATC/ASTNodes/LocalNode/DeclarationNode/DeclarationNode.cs
Normal file
50
YATC/ASTNodes/LocalNode/DeclarationNode/DeclarationNode.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
abstract class DeclarationNode : LocalNode
|
||||||
|
{
|
||||||
|
protected DeclarationNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TigerTypeInfo TigerTypeInfo { get; protected set; }
|
||||||
|
|
||||||
|
public virtual bool CheckHeader(TigerScope scope, Report report, string name)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Por defecto es false
|
||||||
|
/// </summary>
|
||||||
|
public bool IsOK { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
154
YATC/ASTNodes/LocalNode/DeclarationNode/FunDeclSeqNode.cs
Normal file
154
YATC/ASTNodes/LocalNode/DeclarationNode/FunDeclSeqNode.cs
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
internal class FunDeclSeqNode : DeclarationNode
|
||||||
|
{
|
||||||
|
public FunDeclSeqNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunDeclNode[] FunDeclNodes { get { return TigerChildren.Cast<FunDeclNode>().ToArray(); } }
|
||||||
|
|
||||||
|
public ParameterExpression[] FunctionClousures { get; private set; }
|
||||||
|
public Expression[] FunctionAssigns { get; private set; }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
for (int index = 0; index < FunDeclNodes.Length; index++)
|
||||||
|
{
|
||||||
|
var funDeclNode = FunDeclNodes[index];
|
||||||
|
|
||||||
|
if (scope.CanFindFunVarInfo(funDeclNode.Name, true))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Redeclared local function or variable name: '{0}'.", funDeclNode.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FunctionInfo outerFunction = scope.FindFunctionInfo(funDeclNode.Name, false);
|
||||||
|
if (outerFunction != null)
|
||||||
|
if (outerFunction.IsStandard)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Redeclared standard function or procedure: '{0}'.", funDeclNode.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
report.AddWarning(this.Line, this.Column,
|
||||||
|
"Function name hides outer scope variable or function: '{0}'.",
|
||||||
|
funDeclNode.Name);
|
||||||
|
|
||||||
|
// Checking not repiting parameter names
|
||||||
|
var parameterNames = new HashSet<string>();
|
||||||
|
var parameterInfo = new VariableInfo[funDeclNode.Params.Length];
|
||||||
|
for (int i = 0; i < parameterInfo.Length; i++)
|
||||||
|
{
|
||||||
|
var parameter = funDeclNode.Params[i];
|
||||||
|
if (!parameterNames.Add(parameter.Name))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Redeclared function or procedure formal parameter name: '{0}'.", parameter.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
parameter.TypeNode.CheckSemantics(scope, report);
|
||||||
|
TigerTypeInfo paramInfo = scope.FindTypeInfo(parameter.TypeNode.Name, false);
|
||||||
|
|
||||||
|
if (paramInfo == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Undeclared type: Function or procedure parameter: " +
|
||||||
|
"'{0}' of formal parameter '{1}'.", parameter.TypeNode.Name, parameter.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
parameterInfo[i] = new VariableInfo(parameter.Name, paramInfo.Holder, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checking return type
|
||||||
|
TigerType returnType;
|
||||||
|
if (funDeclNode.TypeNode != null)
|
||||||
|
{
|
||||||
|
TigerTypeInfo returnInfo = scope.FindTypeInfo(funDeclNode.TypeNode.Name, false);
|
||||||
|
if (returnInfo == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Undeclared function or procedure return type: {0}", funDeclNode.TypeNode.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
returnType = returnInfo.Holder.TigerType;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
returnType = TigerType.Void;
|
||||||
|
|
||||||
|
var functionInfo = new FunctionInfo(funDeclNode.Name, parameterInfo, new TigerTypeHolder(returnType), false);
|
||||||
|
scope.Add(functionInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checking children
|
||||||
|
bool areAllOk = true;
|
||||||
|
foreach (var funDeclNode in FunDeclNodes)
|
||||||
|
{
|
||||||
|
funDeclNode.CheckSemantics(scope, report);
|
||||||
|
if (!funDeclNode.IsOK)
|
||||||
|
areAllOk = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.IsOK = areAllOk;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
FunctionClousures = new ParameterExpression[this.FunDeclNodes.Length];
|
||||||
|
FunctionAssigns = new Expression[this.FunDeclNodes.Length];
|
||||||
|
for (int i = 0; i < this.FunDeclNodes.Length; i++)
|
||||||
|
FunctionClousures[i] = this.FunDeclNodes[i].GenerateHeaderCode();
|
||||||
|
|
||||||
|
for (int i = 0; i < this.FunDeclNodes.Length; i++)
|
||||||
|
this.FunDeclNodes[i].GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
for (int i = 0; i < this.FunDeclNodes.Length; i++)
|
||||||
|
FunctionAssigns[i] = Expression.Assign(
|
||||||
|
this.FunDeclNodes[i].FunctionInfo.LambdaExpression,
|
||||||
|
this.FunDeclNodes[i].VmExpression
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
144
YATC/ASTNodes/LocalNode/DeclarationNode/FundeclNode.cs
Normal file
144
YATC/ASTNodes/LocalNode/DeclarationNode/FundeclNode.cs
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class FunDeclNode : DeclarationNode
|
||||||
|
{
|
||||||
|
public FunDeclNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||||
|
public TypeFieldNode[] Params
|
||||||
|
{
|
||||||
|
get { return TigerChildren[1].TigerChildren.Cast<TypeFieldNode>().ToArray(); }
|
||||||
|
}
|
||||||
|
public TypeNode TypeNode
|
||||||
|
{
|
||||||
|
get { return TigerChildren[2].TigerChildren.Length > 0 ?
|
||||||
|
(TypeNode)TigerChildren[2].TigerChildren[0] :
|
||||||
|
null; }
|
||||||
|
}
|
||||||
|
public ExpressionNode ExpressionBodyNode { get { return (ExpressionNode)TigerChildren[3]; } }
|
||||||
|
|
||||||
|
public string Name { get { return this.IdNode.Name; } }
|
||||||
|
public FunctionInfo FunctionInfo;
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
FunctionInfo = scope.FindFunctionInfo(this.Name, true);
|
||||||
|
|
||||||
|
if (FunctionInfo == null)
|
||||||
|
throw new NullReferenceException();
|
||||||
|
|
||||||
|
var innerScope = scope.CreateChildScope();
|
||||||
|
foreach (var parameterInfo in FunctionInfo.ParameterInfo)
|
||||||
|
{
|
||||||
|
if (innerScope.CanFindFunVarInfo(parameterInfo.Name, false))
|
||||||
|
report.AddWarning(this.Line, this.Column,
|
||||||
|
"Parameter name hides outer scope variable or function in '{0}': '{1}'.",
|
||||||
|
this.Name, parameterInfo.Name);
|
||||||
|
innerScope.Add(parameterInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.ExpressionBodyNode.CheckSemantics(innerScope, report);
|
||||||
|
|
||||||
|
// chequeando que el tipo de retorno sea el mismo que el tipo de la expresion,
|
||||||
|
// no se hace salvedad en el caso de los procedures.
|
||||||
|
if (!this.ExpressionBodyNode.TigerType.IsAssignableTo(FunctionInfo.Holder.TigerType))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Function or procedure return and expression types in '{0}': " +
|
||||||
|
"Expecting '{1}' and '{2}' found.",
|
||||||
|
this.Name, FunctionInfo.Holder.TigerType.Name, this.ExpressionBodyNode.TigerType.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.IsOK = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Type DelegateType;
|
||||||
|
|
||||||
|
internal ParameterExpression GenerateHeaderCode()
|
||||||
|
{
|
||||||
|
bool funRets = FunctionInfo.Holder.TigerType.Basetype != BaseType.Void;
|
||||||
|
|
||||||
|
Type[] paramTypes = new Type[this.FunctionInfo.ParameterInfo.Length + (funRets ? 1 : 0)];
|
||||||
|
for (int i = 0; i < this.FunctionInfo.ParameterInfo.Length; i++)
|
||||||
|
paramTypes[i] = this.FunctionInfo.ParameterInfo[i].Holder.TigerType.GetCLRType();
|
||||||
|
|
||||||
|
if (funRets)
|
||||||
|
paramTypes[this.Params.Length] = this.FunctionInfo.Holder.TigerType.GetCLRType();
|
||||||
|
|
||||||
|
DelegateType = funRets
|
||||||
|
? Expression.GetFuncType(paramTypes)
|
||||||
|
: Expression.GetActionType(paramTypes);
|
||||||
|
|
||||||
|
var paramExpr = Expression.Parameter(DelegateType);
|
||||||
|
|
||||||
|
FunctionInfo.LambdaExpression = paramExpr;
|
||||||
|
return paramExpr;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
var paramsExprs = new ParameterExpression[this.Params.Length];
|
||||||
|
for (int i = 0; i < Params.Length; i++)
|
||||||
|
{
|
||||||
|
paramsExprs[i] = Expression.Parameter(this.FunctionInfo.ParameterInfo[i].Holder.TigerType.GetCLRType());
|
||||||
|
FunctionInfo.ParameterInfo[i].ParameterExpression = paramsExprs[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
this.ExpressionBodyNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
Expression bodyExpr = this.FunctionInfo.Holder.TigerType.Basetype == BaseType.Void
|
||||||
|
? Expression.Block(
|
||||||
|
this.ExpressionBodyNode.VmExpression,
|
||||||
|
Expression.Empty()
|
||||||
|
)
|
||||||
|
: (Expression)Expression.Convert(
|
||||||
|
ExpressionBodyNode.VmExpression,
|
||||||
|
this.FunctionInfo.Holder.TigerType.GetCLRType()
|
||||||
|
);
|
||||||
|
|
||||||
|
this.VmExpression = Expression.Lambda(
|
||||||
|
//DelegateType,
|
||||||
|
bodyExpr,
|
||||||
|
this.Name,
|
||||||
|
paramsExprs
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
117
YATC/ASTNodes/LocalNode/DeclarationNode/Node.cs
Normal file
117
YATC/ASTNodes/LocalNode/DeclarationNode/Node.cs
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
internal class Node<T>
|
||||||
|
{
|
||||||
|
public enum ColorCode
|
||||||
|
{
|
||||||
|
White = 0,
|
||||||
|
Unvisited = 0,
|
||||||
|
Black = 1,
|
||||||
|
Done = 1,
|
||||||
|
Gray = 2,
|
||||||
|
StillNotDone = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
public ColorCode Color { get; private set; }
|
||||||
|
public Node<T> Parent { get; private set; }
|
||||||
|
public readonly T Value;
|
||||||
|
|
||||||
|
public Node(T value)
|
||||||
|
{
|
||||||
|
this.Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly LinkedList<Node<T>> _pred = new LinkedList<Node<T>>();
|
||||||
|
private readonly LinkedList<Node<T>> _succ = new LinkedList<Node<T>>();
|
||||||
|
|
||||||
|
public IEnumerable<Node<T>> Pred { get { return _pred; } }
|
||||||
|
public IEnumerable<Node<T>> Succ { get { return _succ; } }
|
||||||
|
public IEnumerable<Node<T>> Adj { get { return _pred.Concat(_succ); } }
|
||||||
|
|
||||||
|
public void AddSucc(Node<T> node) { _succ.AddLast(node); }
|
||||||
|
public void AddPred(Node<T> node) { _pred.AddLast(node); }
|
||||||
|
|
||||||
|
private static Node<T> DFS(IEnumerable<Node<T>> nodes, bool checkCycles, out LinkedList<Node<T>> linkedList)
|
||||||
|
{
|
||||||
|
int time = 0;
|
||||||
|
linkedList = new LinkedList<Node<T>>();
|
||||||
|
foreach (var node in nodes.Where(node => node.Color == ColorCode.White))
|
||||||
|
{
|
||||||
|
var cycleEnd = node.Visit(ref time, checkCycles, linkedList);
|
||||||
|
if (cycleEnd != null)
|
||||||
|
return cycleEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Node<T> Visit(ref int time, bool checkCycles, LinkedList<Node<T>> topologicalSort)
|
||||||
|
{
|
||||||
|
this.Color = ColorCode.Gray;
|
||||||
|
foreach (var node in this.Succ)
|
||||||
|
{
|
||||||
|
// by the Proof of Theorem 22.12, page 614, (u,v) is a back edge iff v is Gray
|
||||||
|
// from Cormen et al. - Introduction To Algorithms, 3rd edition
|
||||||
|
if (node.Color == ColorCode.Gray && checkCycles)
|
||||||
|
return this;
|
||||||
|
|
||||||
|
if (node.Color == ColorCode.White)
|
||||||
|
{
|
||||||
|
node.Parent = this;
|
||||||
|
var cycleEnd = node.Visit(ref time, checkCycles, topologicalSort);
|
||||||
|
if (cycleEnd != null)
|
||||||
|
return cycleEnd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.Color = ColorCode.Black;
|
||||||
|
topologicalSort.AddFirst(this);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Node<T> TopologicalSort(IEnumerable<Node<T>> nodes, out LinkedList<Node<T>> linkedList)
|
||||||
|
{
|
||||||
|
return DFS(nodes, true, out linkedList);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NodeValueEqualityComparer : IEqualityComparer<Node<T>>
|
||||||
|
{
|
||||||
|
public bool Equals(Node<T> x, Node<T> y)
|
||||||
|
{
|
||||||
|
return x.Value.Equals(y.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetHashCode(Node<T> obj)
|
||||||
|
{
|
||||||
|
return obj.Value.GetHashCode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
125
YATC/ASTNodes/LocalNode/DeclarationNode/RecordDeclNode.cs
Normal file
125
YATC/ASTNodes/LocalNode/DeclarationNode/RecordDeclNode.cs
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
internal class RecordDeclNode : DeclarationNode
|
||||||
|
{
|
||||||
|
public RecordDeclNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeFieldNode[] TypeFieldNodes
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return TigerChildren.Length > 0 ? TigerChildren.Cast<TypeFieldNode>().ToArray() : new TypeFieldNode[] {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeBuilder TypeBuilder { get; private set; }
|
||||||
|
|
||||||
|
public override bool CheckHeader(TigerScope scope, Report report, string name)
|
||||||
|
{
|
||||||
|
this.TigerTypeInfo = new TigerTypeInfo(name, new TigerTypeHolder(), false);
|
||||||
|
scope.Add(this.TigerTypeInfo);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
var hash = new HashSet<string>();
|
||||||
|
foreach (var typeFieldNode in this.TypeFieldNodes)
|
||||||
|
{
|
||||||
|
typeFieldNode.TypeNode.CheckSemantics(scope, report);
|
||||||
|
|
||||||
|
if (!typeFieldNode.TypeNode.IsOk)
|
||||||
|
{
|
||||||
|
this.TigerTypeInfo.Holder.TigerType = TigerType.Error;
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string name = typeFieldNode.IdNode.Name;
|
||||||
|
if (!hash.Add(name))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Redeclared field name: '{0}'.", name);
|
||||||
|
this.TigerTypeInfo.Holder.TigerType = TigerType.Error;
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VariableInfo[] fieldInfos = new VariableInfo[this.TypeFieldNodes.Length];
|
||||||
|
|
||||||
|
for (int i = 0; i < fieldInfos.Length; i++)
|
||||||
|
{
|
||||||
|
TigerTypeInfo fieldTigerTypeInfo = scope.FindTypeInfo(this.TypeFieldNodes[i].TypeNode.Name, false);
|
||||||
|
if (fieldTigerTypeInfo == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Undeclared field type: '{0}'.",
|
||||||
|
this.TypeFieldNodes[i].TypeNode.Name);
|
||||||
|
this.TigerTypeInfo.Holder.TigerType = TigerType.Error;
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldInfos[i] = new VariableInfo(this.TypeFieldNodes[i].Name, fieldTigerTypeInfo.Holder, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerTypeInfo.Holder.TigerType = new RecordType(this.TigerTypeInfo.Name, fieldInfos);
|
||||||
|
this.IsOK = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GenerateHeaderCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
string name = string.Format("{0}_{1}", this.TigerTypeInfo.Name, ProgramNode.RecordNumber++);
|
||||||
|
this.TigerTypeInfo.Holder.TigerType.CLRType =
|
||||||
|
this.TypeBuilder = moduleBuilder.DefineType(name, TypeAttributes.Public);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
foreach (var variableInfo in ((RecordType)this.TigerTypeInfo.Holder.TigerType).FieldInfos)
|
||||||
|
{
|
||||||
|
this.TypeBuilder.DefineField(
|
||||||
|
variableInfo.Name,
|
||||||
|
variableInfo.Holder.TigerType.GetCLRType(),
|
||||||
|
FieldAttributes.Public);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finish the type.
|
||||||
|
this.TigerTypeInfo.Holder.TigerType.CLRType = this.TypeBuilder.CreateType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
190
YATC/ASTNodes/LocalNode/DeclarationNode/TypeDeclSeqNode.cs
Normal file
190
YATC/ASTNodes/LocalNode/DeclarationNode/TypeDeclSeqNode.cs
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using System.Text;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
internal class TypeDeclSeqNode : DeclarationNode
|
||||||
|
{
|
||||||
|
private LinkedList<Node<TypeDeclNode>> _topologicalSort;
|
||||||
|
|
||||||
|
public TypeDeclSeqNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<TypeDeclNode> TypeDeclNodes { get { return TigerChildren.Cast<TypeDeclNode>(); } }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Por pasos:
|
||||||
|
/// 1ro : Agregar cabezas de tipos
|
||||||
|
/// 2do : Construir grafo y revisar orden topologico
|
||||||
|
/// 3ro : En el orden dado hacer chequeo semantico
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="scope"></param>
|
||||||
|
/// <param name="report"></param>
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
var checkedNodes = new Dictionary<string, TypeDeclNode>();
|
||||||
|
|
||||||
|
// 1. agregar cabezas de tipos
|
||||||
|
foreach (var typeDeclNode in TypeDeclNodes)
|
||||||
|
{
|
||||||
|
if (!checkedNodes.ContainsKey(typeDeclNode.Name))
|
||||||
|
checkedNodes.Add(typeDeclNode.Name, typeDeclNode);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Redeclared name in type declaration sequence: '{0}'.", typeDeclNode.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// chequeamos el header, mayormente por problemas de redeclaracion local
|
||||||
|
// o global pero de tipos standard
|
||||||
|
if (!typeDeclNode.CheckHeader(scope, report, string.Empty))
|
||||||
|
{
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. DAG
|
||||||
|
|
||||||
|
// 2.1 construir grafo
|
||||||
|
|
||||||
|
var graph = new Dictionary<TypeDeclNode, Node<TypeDeclNode>>();
|
||||||
|
|
||||||
|
foreach (var typeDeclNode in TypeDeclNodes)
|
||||||
|
{
|
||||||
|
string edgeNameTo = string.Empty;
|
||||||
|
if (typeDeclNode.IsAliasNode)
|
||||||
|
edgeNameTo = ((AliasDeclNode)typeDeclNode.DeclarationNode).TypeNode.Name;
|
||||||
|
else if (typeDeclNode.IsArrayNode)
|
||||||
|
edgeNameTo = ((ArrayDeclNode)typeDeclNode.DeclarationNode).ElementTypeNode.Name;
|
||||||
|
|
||||||
|
Node<TypeDeclNode> thisNode;
|
||||||
|
if (!graph.TryGetValue(typeDeclNode, out thisNode))
|
||||||
|
{
|
||||||
|
thisNode = new Node<TypeDeclNode>(typeDeclNode);
|
||||||
|
graph.Add(typeDeclNode, thisNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (edgeNameTo == string.Empty)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
TypeDeclNode edgeTo;
|
||||||
|
if (checkedNodes.TryGetValue(edgeNameTo, out edgeTo))
|
||||||
|
{
|
||||||
|
Node<TypeDeclNode> node;
|
||||||
|
if (!graph.TryGetValue(edgeTo, out node))
|
||||||
|
{
|
||||||
|
node = new Node<TypeDeclNode>(edgeTo);
|
||||||
|
graph.Add(edgeTo, node);
|
||||||
|
}
|
||||||
|
|
||||||
|
node.AddSucc(thisNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2.2 obtener orden topologico (detectando ciclos)
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
foreach (var edge in graph.Values)
|
||||||
|
{
|
||||||
|
var from = edge.Succ.Any() ? edge.Succ.FirstOrDefault().Value.IdNode.ToString() : "<end>";
|
||||||
|
var to = edge.Value.IdNode;
|
||||||
|
Debug.WriteLine("{0} -> {1}", from, to);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
var cycleEnd = Node<TypeDeclNode>.TopologicalSort(graph.Values, out _topologicalSort);
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
foreach (var node in _topologicalSort)
|
||||||
|
Debug.WriteLine("Adding {0}", node.Value.IdNode);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (cycleEnd != null)
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
for (var current = cycleEnd; current != null; current = current.Parent)
|
||||||
|
sb.Append(current.Value.IdNode.Name + " -> ");
|
||||||
|
|
||||||
|
sb.Append(cycleEnd.Value.IdNode.Name);
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Undetected record in recursive type declaration sequence. Cycle definition is: {0}",
|
||||||
|
sb.ToString());
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. chequear semantica de los nodos en el orden topologico
|
||||||
|
foreach (var node in _topologicalSort)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("Checking {0}", node.Value.IdNode);
|
||||||
|
node.Value.CheckSemantics(scope, report);
|
||||||
|
if (!node.Value.IsOK)
|
||||||
|
{
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//foreach (var node in linkedList)
|
||||||
|
//{
|
||||||
|
// node.
|
||||||
|
//}
|
||||||
|
|
||||||
|
this.IsOK = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
// records go first
|
||||||
|
|
||||||
|
IEnumerable<RecordDeclNode> recordDeclNodes =
|
||||||
|
_topologicalSort.Select(x => x.Value).
|
||||||
|
Where(x => x.DeclarationNode is RecordDeclNode).
|
||||||
|
Select(x => x.DeclarationNode).
|
||||||
|
Cast<RecordDeclNode>();
|
||||||
|
|
||||||
|
foreach (var recordDeclNode in recordDeclNodes)
|
||||||
|
recordDeclNode.GenerateHeaderCode(moduleBuilder);
|
||||||
|
|
||||||
|
// everything
|
||||||
|
|
||||||
|
foreach (var declarationNode in _topologicalSort.Select(x => x.Value))
|
||||||
|
declarationNode.GenerateCode(moduleBuilder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
84
YATC/ASTNodes/LocalNode/DeclarationNode/TypedeclNode.cs
Normal file
84
YATC/ASTNodes/LocalNode/DeclarationNode/TypedeclNode.cs
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class TypeDeclNode : DeclarationNode
|
||||||
|
{
|
||||||
|
public TypeDeclNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name { get { return this.IdNode.Name; } }
|
||||||
|
|
||||||
|
public IdNode IdNode { get { return (IdNode)TigerChildren[0]; } }
|
||||||
|
public DeclarationNode DeclarationNode { get { return (DeclarationNode)TigerChildren[1]; } }
|
||||||
|
|
||||||
|
public bool IsAliasNode { get { return this.DeclarationNode is AliasDeclNode; } }
|
||||||
|
public bool IsRecordNode { get { return this.DeclarationNode is RecordDeclNode; } }
|
||||||
|
public bool IsArrayNode { get { return this.DeclarationNode is ArrayDeclNode; } }
|
||||||
|
|
||||||
|
public override bool CheckHeader(TigerScope scope, Report report, string name)
|
||||||
|
{
|
||||||
|
if (scope.CanFindTypeInfo(this.Name, true))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Redeclared local type: '{0}'.", this.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
TigerTypeInfo outerTypeInfo = scope.FindTypeInfo(this.Name, false);
|
||||||
|
if (outerTypeInfo != null)
|
||||||
|
if (outerTypeInfo.IsStandard)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Redeclared standard type: '{0}'.",
|
||||||
|
this.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
report.AddWarning(this.Line, this.Column, "Type name hides outer scope type: '{0}'.", this.Name);
|
||||||
|
|
||||||
|
return this.DeclarationNode.CheckHeader(scope, report, this.IdNode.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.DeclarationNode.CheckSemantics(scope, report);
|
||||||
|
this.IsOK = this.DeclarationNode.IsOK;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.DeclarationNode.GenerateCode(moduleBuilder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
140
YATC/ASTNodes/LocalNode/DeclarationNode/VardeclNode.cs
Normal file
140
YATC/ASTNodes/LocalNode/DeclarationNode/VardeclNode.cs
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class VarDeclNode : DeclarationNode
|
||||||
|
{
|
||||||
|
public VarDeclNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||||
|
public TypeNode TypeNode { get { return (TypeNode)TigerChildren[1]; } }
|
||||||
|
public ExpressionNode RightExpressionNode { get { return (ExpressionNode)TigerChildren[2]; } }
|
||||||
|
|
||||||
|
public VariableInfo VariableInfo { get; set; }
|
||||||
|
public bool IsAutoVariable { get { return this.TypeNode is FillInTypeNode; } }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
string name = this.IdNode.Name;
|
||||||
|
|
||||||
|
if (scope.CanFindFunVarInfo(name, true))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Redeclared local variable or function: '{0}'.",
|
||||||
|
name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FunctionInfo outerfunctionInfo = scope.FindFunctionInfo(name, false);
|
||||||
|
if (outerfunctionInfo != null)
|
||||||
|
{
|
||||||
|
if (outerfunctionInfo.IsStandard)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Cannot define variable name with standard function: '{0}'.",
|
||||||
|
name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
report.AddWarning(this.Line, this.Column,
|
||||||
|
"Variable name hides outer scope variable or function: '{0}'.",
|
||||||
|
name);
|
||||||
|
}
|
||||||
|
this.RightExpressionNode.CheckSemantics(scope, report);
|
||||||
|
|
||||||
|
if (!this.RightExpressionNode.IsOk)
|
||||||
|
{
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.RightExpressionNode.TigerType.Equals(TigerType.Void))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Right hand side expression must evaluate to a returning value.");
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TypeNode.CheckSemantics(scope, report);
|
||||||
|
TigerType returnType = this.RightExpressionNode.TigerType;
|
||||||
|
|
||||||
|
if (!this.IsAutoVariable)
|
||||||
|
{
|
||||||
|
TigerTypeInfo tigerTypeInfo = scope.FindTypeInfo(this.TypeNode.Name, false);
|
||||||
|
if (tigerTypeInfo == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Undeclared type: '{0}'.", this.TypeNode.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Assignment */
|
||||||
|
if (!this.RightExpressionNode.TigerType.IsAssignableTo(tigerTypeInfo.Holder.TigerType))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Variable declaration and expression do not match " +
|
||||||
|
"for '{0}': expecting '{1}' and '{2}' found.",
|
||||||
|
name, tigerTypeInfo.Holder.TigerType.Name, this.RightExpressionNode.TigerType.Name);
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
returnType = tigerTypeInfo.Holder.TigerType;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (returnType.Basetype == BaseType.Nil)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"An automatic variable cannot be declared from nil expression.");
|
||||||
|
this.IsOK = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.VariableInfo = new VariableInfo(name, new TigerTypeHolder(returnType), false);
|
||||||
|
scope.Add(VariableInfo);
|
||||||
|
this.IsOK = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.RightExpressionNode.GenerateCode(moduleBuilder);
|
||||||
|
this.VariableInfo.ParameterExpression =
|
||||||
|
Expression.Parameter(this.VariableInfo.Holder.TigerType.GetCLRType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
144
YATC/ASTNodes/LocalNode/ExpressionNode/ArrayInstNode.cs
Normal file
144
YATC/ASTNodes/LocalNode/ExpressionNode/ArrayInstNode.cs
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class ArrayInstNode : ExpressionNode
|
||||||
|
{
|
||||||
|
private ArrayType _arrayType;
|
||||||
|
|
||||||
|
public ArrayInstNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public IdNode IdNode { get { return (IdNode)TigerChildren[0]; } }
|
||||||
|
public ExpressionNode IndexExpressionNode { get { return (ExpressionNode)TigerChildren[1]; } }
|
||||||
|
public ExpressionNode OfExpressionNode { get { return (ExpressionNode)TigerChildren[2]; } }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
TigerTypeInfo arrayInfo = scope.FindTypeInfo(this.IdNode.Name, false);
|
||||||
|
if (arrayInfo == null || arrayInfo.Holder == null || arrayInfo.Holder.TigerType == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Undeclared array type: '{0}'.", this.IdNode.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arrayInfo.Holder.TigerType.Basetype != BaseType.Array)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Given type is not an array: {0}", this.IdNode.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_arrayType = arrayInfo.Holder.TigerType as ArrayType;
|
||||||
|
if (_arrayType == null)
|
||||||
|
throw new NullReferenceException();
|
||||||
|
|
||||||
|
this.IndexExpressionNode.CheckSemantics(scope, report);
|
||||||
|
if (!this.IndexExpressionNode.IsOk)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.IndexExpressionNode.TigerType.Basetype != BaseType.Int)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Type mismatch: Given index expression is not an integer: {0}", this.IndexExpressionNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.OfExpressionNode.CheckSemantics(scope, report);
|
||||||
|
if (!this.OfExpressionNode.IsOk)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.OfExpressionNode.TigerType.IsAssignableTo(_arrayType.ElementTypeHolder.TigerType))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Type mismatch: Array element and expression types: '{0}' and '{1}'",
|
||||||
|
_arrayType.ElementTypeHolder.TigerType.Name, this.OfExpressionNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = _arrayType;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.IndexExpressionNode.GenerateCode(moduleBuilder);
|
||||||
|
this.OfExpressionNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
Type elementType = _arrayType.ElementTypeHolder.TigerType.GetCLRType();
|
||||||
|
Type arrayType = elementType.MakeArrayType();
|
||||||
|
ParameterExpression arrayParamExpr = Expression.Parameter(arrayType);
|
||||||
|
ParameterExpression counterExpr = Expression.Parameter(typeof(int));
|
||||||
|
|
||||||
|
var arrayInitExpr = Expression.NewArrayBounds(elementType, this.IndexExpressionNode.VmExpression);
|
||||||
|
|
||||||
|
LabelTarget breakLabel = Expression.Label();
|
||||||
|
|
||||||
|
BlockExpression blockInitExpression = Expression.Block(
|
||||||
|
new ParameterExpression[] { arrayParamExpr, counterExpr },
|
||||||
|
new Expression[]
|
||||||
|
{
|
||||||
|
Expression.Assign(arrayParamExpr, arrayInitExpr),
|
||||||
|
Expression.Assign(counterExpr, Expression.Constant(0)),
|
||||||
|
Expression.Loop(
|
||||||
|
Expression.Block(
|
||||||
|
Expression.IfThenElse(
|
||||||
|
Expression.LessThan(
|
||||||
|
counterExpr,
|
||||||
|
this.IndexExpressionNode.VmExpression),
|
||||||
|
Expression.Assign(
|
||||||
|
Expression.ArrayAccess(arrayParamExpr, counterExpr),
|
||||||
|
Expression.Convert(
|
||||||
|
this.OfExpressionNode.VmExpression,
|
||||||
|
elementType
|
||||||
|
)
|
||||||
|
),
|
||||||
|
Expression.Break(breakLabel)
|
||||||
|
),
|
||||||
|
Expression.Assign(counterExpr, Expression.Increment(counterExpr))
|
||||||
|
), breakLabel),
|
||||||
|
arrayParamExpr
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
this.VmExpression = blockInitExpression;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
public abstract class AccessNode: AtomicNode
|
||||||
|
{
|
||||||
|
protected AccessNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TigerType ParentType { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
public class ArrayAccessNode : AccessNode
|
||||||
|
{
|
||||||
|
public ArrayAccessNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExpressionNode IndexExpressionNode { get { return (ExpressionNode) TigerChildren[0]; } }
|
||||||
|
public AccessNode AccessNode { get { return TigerChildren.Length > 1 ? (AccessNode)TigerChildren[1] : null; } }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.IndexExpressionNode.CheckSemantics(scope, report);
|
||||||
|
if (!this.IndexExpressionNode.IsOk)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.IndexExpressionNode.TigerType.Basetype != BaseType.Int)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Cannot index with a non-integer expression: '{0}' was found.",
|
||||||
|
this.IndexExpressionNode.TigerType.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.ParentType.Basetype != BaseType.Array)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Variable or field is not an array: '{0}' was found.",
|
||||||
|
this.ParentType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
TigerType parentType = ((ArrayType)this.ParentType).ElementTypeHolder.TigerType;
|
||||||
|
if (this.AccessNode == null)
|
||||||
|
{
|
||||||
|
this.TigerType = parentType;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.AccessNode.ParentType = parentType;
|
||||||
|
this.AccessNode.CheckSemantics(scope, report);
|
||||||
|
this.TigerType = AccessNode.TigerType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.IndexExpressionNode.GenerateCode(moduleBuilder);
|
||||||
|
this.VmExpression = Expression.ArrayAccess(
|
||||||
|
this.VmExpression,
|
||||||
|
this.IndexExpressionNode.VmExpression
|
||||||
|
);
|
||||||
|
|
||||||
|
if (this.AccessNode == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.AccessNode.VmExpression = this.VmExpression;
|
||||||
|
this.AccessNode.GenerateCode(moduleBuilder);
|
||||||
|
this.VmExpression = this.AccessNode.VmExpression;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class FieldAccessNode : AccessNode
|
||||||
|
{
|
||||||
|
private TigerType _parentType;
|
||||||
|
|
||||||
|
public FieldAccessNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||||
|
public AccessNode AccessNode { get { return TigerChildren.Length > 1 ? (AccessNode)TigerChildren[1] : null; } }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
if (this.ParentType.Basetype != BaseType.Record)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Variable or field is not a record: '{0}' was found.",
|
||||||
|
this.ParentType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
VariableInfo[] fieldInfos = ((RecordType)this.ParentType).FieldInfos;
|
||||||
|
if (fieldInfos.Length == 0)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Cannot access field on empty record type '{0}': '{1}'",
|
||||||
|
this.ParentType.Name, this.IdNode.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var fieldInfo = fieldInfos.FirstOrDefault(x => x.Name == this.IdNode.Name);
|
||||||
|
|
||||||
|
if (fieldInfo == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Record type '{0}' does not contain a definition for '{1}'.",
|
||||||
|
this.ParentType.Name, this.IdNode.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_parentType = fieldInfo.Holder.TigerType;
|
||||||
|
|
||||||
|
if (this.AccessNode == null)
|
||||||
|
{
|
||||||
|
this.TigerType = _parentType;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.AccessNode.ParentType = _parentType;
|
||||||
|
this.AccessNode.CheckSemantics(scope, report);
|
||||||
|
this.TigerType = AccessNode.TigerType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.VmExpression = Expression.MakeMemberAccess(
|
||||||
|
this.VmExpression,
|
||||||
|
this.ParentType.GetCLRType().GetMember(this.IdNode.Name)[0]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (this.AccessNode == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.AccessNode.VmExpression = this.VmExpression;
|
||||||
|
this.AccessNode.GenerateCode(moduleBuilder);
|
||||||
|
this.VmExpression = this.AccessNode.VmExpression;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
public abstract class AtomicNode : ExpressionNode
|
||||||
|
{
|
||||||
|
protected AtomicNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class ExprSeqNode : AtomicNode
|
||||||
|
{
|
||||||
|
public ExprSeqNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Says whether the node contains a BreakNode, that is not within the control of a cycle control structure.
|
||||||
|
/// </summary>
|
||||||
|
public bool HasBreakInside { get; internal set; }
|
||||||
|
|
||||||
|
public ExpressionNode[] ExpressionNodes
|
||||||
|
{
|
||||||
|
get { return TigerChildren.Length > 0 ?
|
||||||
|
TigerChildren.Cast<ExpressionNode>().ToArray() : null; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
if (this.ExpressionNodes == null)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Void;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool allOk = true;
|
||||||
|
foreach (var expressionNode in this.ExpressionNodes)
|
||||||
|
{
|
||||||
|
expressionNode.CheckSemantics(scope, report);
|
||||||
|
if (!expressionNode.IsOk)
|
||||||
|
allOk = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = allOk
|
||||||
|
? ((this.HasBreakInside || ExpressionNodes.Length == 0)
|
||||||
|
? TigerType.Void
|
||||||
|
: ExpressionNodes[ExpressionNodes.Length - 1].TigerType)
|
||||||
|
: TigerType.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
if (this.ExpressionNodes != null)
|
||||||
|
{
|
||||||
|
foreach (var expressionNode in this.ExpressionNodes)
|
||||||
|
expressionNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
this.VmExpression = Expression.Block(new ParameterExpression[] { },
|
||||||
|
this.ExpressionNodes.Select(x => x.VmExpression ?? Expression.Empty()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
this.VmExpression = Expression.Empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
137
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/FuncallNode.cs
Normal file
137
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/FuncallNode.cs
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class FunCallNode : AtomicNode
|
||||||
|
{
|
||||||
|
public FunCallNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||||
|
|
||||||
|
public ExpressionNode[] ActualParametersNodes
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return TigerChildren.Length > 1 ?
|
||||||
|
TigerChildren[1].TigerChildren.Cast<ExpressionNode>().ToArray() :
|
||||||
|
new ExpressionNode[] {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionInfo FunctionInfo { get; set; }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
FunctionInfo functionInfo = scope.FindFunctionInfo(this.IdNode.Name, false);
|
||||||
|
this.FunctionInfo = functionInfo;
|
||||||
|
|
||||||
|
if (functionInfo == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Undeclared function: '{0}'.", this.IdNode.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.ActualParametersNodes == null && functionInfo.ParameterInfo.Length > 0 ||
|
||||||
|
this.ActualParametersNodes != null && this.ActualParametersNodes.Length != functionInfo.ParameterInfo.Length)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Length of formal and actual parameters differ for function or procedure '{0}': " +
|
||||||
|
"Expecting {1} and found {2} arguments.",
|
||||||
|
this.IdNode.Name,
|
||||||
|
functionInfo.ParameterInfo.Length,
|
||||||
|
this.ActualParametersNodes != null ? this.ActualParametersNodes.Length : 0);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.ActualParametersNodes != null)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < functionInfo.ParameterInfo.Length; i++)
|
||||||
|
{
|
||||||
|
this.ActualParametersNodes[i].CheckSemantics(scope, report);
|
||||||
|
if (!this.ActualParametersNodes[i].IsOk)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.ActualParametersNodes[i].TigerType.IsAssignableTo(functionInfo.ParameterInfo[i].Holder.TigerType))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Types mismatch: Formal and actual parameter types differ for argument number {0} whilst calling " +
|
||||||
|
"function or procedure '{3}': Expecting '{1}' and found '{2}'.",
|
||||||
|
i,
|
||||||
|
functionInfo.ParameterInfo[i].Holder.TigerType.Name,
|
||||||
|
this.ActualParametersNodes[i].TigerType.Name,
|
||||||
|
this.IdNode.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = functionInfo.Holder.TigerType;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
Expression[] arguments = new Expression[this.ActualParametersNodes.Length];
|
||||||
|
for (int i = 0; i < this.ActualParametersNodes.Length; i++)
|
||||||
|
{
|
||||||
|
ExpressionNode argument = this.ActualParametersNodes[i];
|
||||||
|
argument.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
arguments[i] = Expression.Convert(
|
||||||
|
argument.VmExpression,
|
||||||
|
this.FunctionInfo.ParameterInfo[i].Holder.TigerType.GetCLRType()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.FunctionInfo.Name == "exit")
|
||||||
|
{
|
||||||
|
var exit = ((Action<int>)Environment.Exit).Method;
|
||||||
|
this.VmExpression = Expression.Call(exit, arguments[0]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.FunctionInfo.MethodInfo != null)
|
||||||
|
this.VmExpression = Expression.Call(this.FunctionInfo.MethodInfo, arguments);
|
||||||
|
else
|
||||||
|
this.VmExpression = Expression.Invoke(this.FunctionInfo.LambdaExpression, arguments);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
138
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/IfNode.cs
Normal file
138
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/IfNode.cs
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class IfNode : AtomicNode
|
||||||
|
{
|
||||||
|
public IfNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExpressionNode ConditionNode { get { return (ExpressionNode)TigerChildren[0]; } }
|
||||||
|
public ExpressionNode ThenExpressionNode { get { return (ExpressionNode)TigerChildren[1]; } }
|
||||||
|
public ExpressionNode ElseExpressionNode
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return TigerChildren.Length > 2 ? (ExpressionNode)TigerChildren[2] : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.ConditionNode.CheckSemantics(scope, report);
|
||||||
|
this.ThenExpressionNode.CheckSemantics(scope, report);
|
||||||
|
bool allOk = this.ConditionNode.IsOk && this.ThenExpressionNode.IsOk;
|
||||||
|
|
||||||
|
if (this.ConditionNode.IsOk)
|
||||||
|
{
|
||||||
|
if (this.ConditionNode.TigerType.Basetype == BaseType.Void)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of if condition with a non-valued expression.",
|
||||||
|
this.ConditionNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
allOk = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if (this.ConditionNode.TigerType.Basetype != BaseType.Int)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of if condition with a non-int expression: '{0}' was found.",
|
||||||
|
this.ConditionNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
allOk = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TigerType returnType = this.ThenExpressionNode.TigerType;
|
||||||
|
|
||||||
|
if (this.ElseExpressionNode != null)
|
||||||
|
{
|
||||||
|
this.ElseExpressionNode.CheckSemantics(scope, report);
|
||||||
|
allOk &= this.ElseExpressionNode.IsOk;
|
||||||
|
|
||||||
|
bool isThenAssignable =
|
||||||
|
this.ThenExpressionNode.TigerType.IsAssignableTo(this.ElseExpressionNode.TigerType);
|
||||||
|
|
||||||
|
returnType = isThenAssignable ? this.ElseExpressionNode.TigerType : this.ThenExpressionNode.TigerType;
|
||||||
|
|
||||||
|
/* facilita que se puede asignar record y nil, con independencia del orden en que aparezca */
|
||||||
|
if (!isThenAssignable && !this.ElseExpressionNode.TigerType.IsAssignableTo(this.ThenExpressionNode.TigerType))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: The then and else expression types of an if-then-else differ: " +
|
||||||
|
"'{0}' and '{1}' were found.",
|
||||||
|
this.ThenExpressionNode.TigerType.Name, this.ElseExpressionNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (this.ThenExpressionNode.TigerType.Basetype != BaseType.Void)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: The then expression at a if-then statement must not return a value: " +
|
||||||
|
"Found '{0}' whilst expecting void.",
|
||||||
|
this.ThenExpressionNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = allOk ? returnType : TigerType.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.ConditionNode.GenerateCode(moduleBuilder);
|
||||||
|
this.ThenExpressionNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
if (this.ElseExpressionNode != null)
|
||||||
|
{
|
||||||
|
this.ElseExpressionNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
this.VmExpression = Expression.Condition(
|
||||||
|
Expression.NotEqual(this.ConditionNode.VmExpression, Expression.Constant(0)),
|
||||||
|
this.ThenExpressionNode.VmExpression,
|
||||||
|
this.ElseExpressionNode.VmExpression);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.VmExpression = Expression.IfThen(
|
||||||
|
Expression.NotEqual(this.ConditionNode.VmExpression, Expression.Constant(0)),
|
||||||
|
this.ThenExpressionNode.VmExpression);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
58
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/IntNode.cs
Normal file
58
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/IntNode.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class IntNode : AtomicNode
|
||||||
|
{
|
||||||
|
public IntNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Value { get { return int.Parse(this.Text); } }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
int value;
|
||||||
|
if (!int.TryParse(this.Text, out value))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Integer literal value is out of range.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
}
|
||||||
|
else this.TigerType = TigerType.Int;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.VmExpression = Expression.Constant(this.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
131
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/LetNode.cs
Normal file
131
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/LetNode.cs
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class LetNode : AtomicNode
|
||||||
|
{
|
||||||
|
public LetNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<DeclarationNode> DeclarationNodes { get { return TigerChildren[0].TigerChildren.Cast<DeclarationNode>(); } }
|
||||||
|
public ExprSeqNode ExprSeqNode
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return (ExprSeqNode)TigerChildren[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
TigerScope innerScope = scope.CreateChildScope();
|
||||||
|
|
||||||
|
foreach (var declarationNode in this.DeclarationNodes)
|
||||||
|
{
|
||||||
|
declarationNode.CheckSemantics(innerScope, report);
|
||||||
|
if (!declarationNode.IsOK)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.ExprSeqNode.CheckSemantics(innerScope, report);
|
||||||
|
if (!this.ExprSeqNode.IsOk || scope.ContainsType(this.ExprSeqNode.TigerType, false))
|
||||||
|
this.TigerType = this.ExprSeqNode.TigerType;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Type '{0}' returned from let declaration is not " +
|
||||||
|
"defined in an outer scope, or it is a different definition.",
|
||||||
|
this.ExprSeqNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
foreach (var declarationNode in DeclarationNodes)
|
||||||
|
declarationNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
if (this.ExprSeqNode.ExpressionNodes != null)
|
||||||
|
foreach (var expressionNode in this.ExprSeqNode.ExpressionNodes)
|
||||||
|
expressionNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
// variables
|
||||||
|
IEnumerable<VarDeclNode> varDeclNodes =
|
||||||
|
this.DeclarationNodes.Where(x => x is VarDeclNode).Cast<VarDeclNode>();
|
||||||
|
|
||||||
|
IEnumerable<ParameterExpression> variables =
|
||||||
|
varDeclNodes.Select(x => x.VariableInfo.ParameterExpression);
|
||||||
|
|
||||||
|
IEnumerable<Expression> initVariablesExpressions =
|
||||||
|
varDeclNodes.Select(varDeclNode =>
|
||||||
|
Expression.Assign(
|
||||||
|
varDeclNode.VariableInfo.ParameterExpression,
|
||||||
|
Expression.Convert(
|
||||||
|
varDeclNode.RightExpressionNode.VmExpression,
|
||||||
|
varDeclNode.VariableInfo.Holder.TigerType.GetCLRType()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// fundeclseq
|
||||||
|
IEnumerable<FunDeclSeqNode> funDeclSeqNodes =
|
||||||
|
this.DeclarationNodes.Where(x => x is FunDeclSeqNode).Cast<FunDeclSeqNode>();
|
||||||
|
|
||||||
|
// final
|
||||||
|
IEnumerable<Expression> blockExpressions = initVariablesExpressions.Concat(
|
||||||
|
this.ExprSeqNode.ExpressionNodes != null ?
|
||||||
|
(this.ExprSeqNode.ExpressionNodes.Select(x => x.VmExpression)) :
|
||||||
|
new Expression[] { Expression.Empty() }
|
||||||
|
);
|
||||||
|
|
||||||
|
var functionClousures = new List<ParameterExpression>();
|
||||||
|
var functionAssigns = new List<Expression>();
|
||||||
|
foreach (var funDeclSeqNode in funDeclSeqNodes)
|
||||||
|
{
|
||||||
|
functionClousures.AddRange(funDeclSeqNode.FunctionClousures);
|
||||||
|
functionAssigns.AddRange(funDeclSeqNode.FunctionAssigns);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.VmExpression = Expression.Block(
|
||||||
|
functionClousures.Concat(variables),
|
||||||
|
functionAssigns.Concat(blockExpressions));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
50
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/NilNode.cs
Normal file
50
YATC/ASTNodes/LocalNode/ExpressionNode/AtomicNode/NilNode.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class NilNode: AtomicNode
|
||||||
|
{
|
||||||
|
public NilNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.VmExpression = Expression.Constant(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class StringNode : AtomicNode
|
||||||
|
{
|
||||||
|
public StringNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
string tmp = Text.Substring(1, Text.Length - 2);
|
||||||
|
tmp = Regex.Replace(tmp, @"\\(\n|\r|\t|\s)+\\", string.Empty);
|
||||||
|
tmp = Regex.Replace(tmp, @"(\\\d\d\d)", ToAscii);
|
||||||
|
Value = Regex.Unescape(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Value { get; private set; }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.String;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.VmExpression = Expression.Constant(this.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string ToAscii(Match m)
|
||||||
|
{
|
||||||
|
var a = int.Parse(m.Groups[0].Value.Substring(1));
|
||||||
|
return Convert.ToChar(a).ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
internal class VarAccessNode : AtomicNode
|
||||||
|
{
|
||||||
|
public VarAccessNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||||
|
public AccessNode AccessNode { get { return TigerChildren.Length > 1 ? (AccessNode)TigerChildren[1] : null; } }
|
||||||
|
|
||||||
|
public string Name { get { return IdNode.Text; } }
|
||||||
|
public VariableInfo VariableInfo { get; set; }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.VariableInfo = scope.FindVariableInfo(this.Name, false);
|
||||||
|
if (this.VariableInfo == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Undeclared variable: '{0}'.", this.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.AccessNode != null)
|
||||||
|
{
|
||||||
|
this.AccessNode.ParentType = this.VariableInfo.Holder.TigerType;
|
||||||
|
this.AccessNode.CheckSemantics(scope, report);
|
||||||
|
this.TigerType = this.AccessNode.TigerType;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = VariableInfo.Holder.TigerType;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.VmExpression = this.VariableInfo.ParameterExpression;
|
||||||
|
|
||||||
|
if (this.AccessNode == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.AccessNode.VmExpression = this.VmExpression;
|
||||||
|
this.AccessNode.GenerateCode(moduleBuilder);
|
||||||
|
this.VmExpression = this.AccessNode.VmExpression;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
abstract class ArithmeticNode : BinaryNode
|
||||||
|
{
|
||||||
|
protected ArithmeticNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
base.CheckSemantics(scope, report);
|
||||||
|
bool bothOk = this.LeftOperandNode.IsOk && this.RightOperandNode.IsOk;
|
||||||
|
|
||||||
|
if (this.LeftOperandNode.IsOk)
|
||||||
|
{
|
||||||
|
if (this.LeftOperandNode.TigerType.Basetype == BaseType.Void)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary arithmetic operator with a non-valued left expression.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if (this.LeftOperandNode.TigerType.Basetype != BaseType.Int)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary arithmetic operator with a non-int left expression: '{0}' was found.",
|
||||||
|
this.LeftOperandNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.RightOperandNode.IsOk)
|
||||||
|
{
|
||||||
|
if (this.RightOperandNode.TigerType.Basetype == BaseType.Void)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary arithmetic operator with a non-valued right expression.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if (this.RightOperandNode.TigerType.Basetype != BaseType.Int)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary arithmetic operator with a non-int right expression: '{0}' was found.",
|
||||||
|
this.RightOperandNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.TigerType = bothOk ? TigerType.Int : TigerType.Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using Antlr.Runtime;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class DivNode : ArithmeticNode
|
||||||
|
{
|
||||||
|
public DivNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.Divide;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using Antlr.Runtime;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class MinusNode : ArithmeticNode
|
||||||
|
{
|
||||||
|
public MinusNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.Subtract;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using Antlr.Runtime;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class MultNode : ArithmeticNode
|
||||||
|
{
|
||||||
|
public MultNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.Multiply;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using Antlr.Runtime;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class PlusNode : ArithmeticNode
|
||||||
|
{
|
||||||
|
public PlusNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.Add;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
abstract class BinaryNode : ExpressionNode
|
||||||
|
{
|
||||||
|
public BinaryNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ExpressionType ExpressionType;
|
||||||
|
|
||||||
|
public ExpressionNode LeftOperandNode { get { return (ExpressionNode)TigerChildren[0]; } }
|
||||||
|
public ExpressionNode RightOperandNode { get { return (ExpressionNode)TigerChildren[1]; } }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.LeftOperandNode.CheckSemantics(scope, report);
|
||||||
|
this.RightOperandNode.CheckSemantics(scope, report);
|
||||||
|
|
||||||
|
if (!this.LeftOperandNode.IsOk || !this.RightOperandNode.IsOk)
|
||||||
|
// No se reporta el error para evitar cascadas de errores
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.LeftOperandNode.GenerateCode(moduleBuilder);
|
||||||
|
this.RightOperandNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
this.VmExpression = Expression.MakeBinary(this.ExpressionType,
|
||||||
|
this.LeftOperandNode.VmExpression,
|
||||||
|
this.RightOperandNode.VmExpression
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class AndNode : LogicNode
|
||||||
|
{
|
||||||
|
public AndNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.AndAlso;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
abstract class LogicNode : BinaryNode
|
||||||
|
{
|
||||||
|
protected LogicNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
base.CheckSemantics(scope, report);
|
||||||
|
bool bothOk = this.LeftOperandNode.IsOk && this.RightOperandNode.IsOk;
|
||||||
|
|
||||||
|
if (this.LeftOperandNode.IsOk)
|
||||||
|
{
|
||||||
|
if (this.LeftOperandNode.TigerType.Basetype == BaseType.Void)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary logical operator with a non-valued left expression.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if (this.LeftOperandNode.TigerType.Basetype != BaseType.Int)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary logical operator with a non-int left expression: '{0}' was found.",
|
||||||
|
this.LeftOperandNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.RightOperandNode.IsOk)
|
||||||
|
{
|
||||||
|
if (this.RightOperandNode.TigerType.Basetype == BaseType.Void)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary logical operator with a non-valued right expression.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if (this.RightOperandNode.TigerType.Basetype != BaseType.Int)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary logical operator with a non-int right expression: '{0}' was found.",
|
||||||
|
this.RightOperandNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = bothOk ? TigerType.Int : TigerType.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.LeftOperandNode.GenerateCode(moduleBuilder);
|
||||||
|
this.RightOperandNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
this.LeftOperandNode.VmExpression = Expression.NotEqual(this.LeftOperandNode.VmExpression,
|
||||||
|
Expression.Constant(0));
|
||||||
|
this.RightOperandNode.VmExpression = Expression.NotEqual(this.RightOperandNode.VmExpression,
|
||||||
|
Expression.Constant(0));
|
||||||
|
|
||||||
|
this.VmExpression = Expression.MakeBinary(this.ExpressionType,
|
||||||
|
this.LeftOperandNode.VmExpression,
|
||||||
|
this.RightOperandNode.VmExpression
|
||||||
|
);
|
||||||
|
|
||||||
|
this.VmExpression = Expression.Condition(this.VmExpression, Expression.Constant(1), Expression.Constant(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using Antlr.Runtime;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class OrNode : LogicNode
|
||||||
|
{
|
||||||
|
public OrNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.OrElse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class EqNode : IdentityNode
|
||||||
|
{
|
||||||
|
public EqNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.Equal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
abstract class IdentityNode : RelationalNode
|
||||||
|
{
|
||||||
|
protected IdentityNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
base.CheckSemantics(scope, report);
|
||||||
|
bool bothOk = this.LeftOperandNode.IsOk && this.RightOperandNode.IsOk;
|
||||||
|
|
||||||
|
if (LeftOperandNode.IsOk && this.LeftOperandNode.TigerType.Basetype == BaseType.Void)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary identity operator with a non-valued left expression.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.RightOperandNode.IsOk && this.RightOperandNode.TigerType.Basetype == BaseType.Void)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary identity operator with a non-valued right expression.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.LeftOperandNode.TigerType.Basetype == BaseType.Nil &&
|
||||||
|
this.RightOperandNode.TigerType.Basetype == BaseType.Nil)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary identity operator with two nils.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bothOk &&
|
||||||
|
!LeftOperandNode.TigerType.IsAssignableTo(RightOperandNode.TigerType) &&
|
||||||
|
!RightOperandNode.TigerType.IsAssignableTo(LeftOperandNode.TigerType))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary identity operator with different types: '{0}' and '{1}' were found.",
|
||||||
|
this.LeftOperandNode.TigerType.Name,
|
||||||
|
this.RightOperandNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = bothOk ? TigerType.Int : TigerType.Error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class NoteqNode : IdentityNode
|
||||||
|
{
|
||||||
|
public NoteqNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.NotEqual;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Mayor o igual que ">="
|
||||||
|
/// </summary>
|
||||||
|
class GteqNode : OrderNode
|
||||||
|
{
|
||||||
|
public GteqNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.GreaterThanOrEqual;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Mayor que ">"
|
||||||
|
/// </summary>
|
||||||
|
class GtNode : OrderNode
|
||||||
|
{
|
||||||
|
public GtNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.GreaterThan;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Menor o igual "<="
|
||||||
|
/// </summary>
|
||||||
|
class LteqNode : OrderNode
|
||||||
|
{
|
||||||
|
public LteqNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.LessThanOrEqual;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Menor que "<"
|
||||||
|
/// </summary>
|
||||||
|
class LtNode : OrderNode
|
||||||
|
{
|
||||||
|
public LtNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
this.ExpressionType = ExpressionType.LessThan;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
abstract class OrderNode : RelationalNode
|
||||||
|
{
|
||||||
|
protected OrderNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
base.CheckSemantics(scope, report);
|
||||||
|
bool bothOk = this.LeftOperandNode.IsOk && this.RightOperandNode.IsOk;
|
||||||
|
|
||||||
|
if (this.LeftOperandNode.IsOk)
|
||||||
|
{
|
||||||
|
if (this.LeftOperandNode.TigerType.Basetype == BaseType.Void)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary order operator with a non-valued left expression.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if (this.LeftOperandNode.TigerType.Basetype != BaseType.Int &&
|
||||||
|
this.LeftOperandNode.TigerType.Basetype != BaseType.String)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary order operator with a non-int or non-string left expression: '{0}' was found.",
|
||||||
|
this.LeftOperandNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.RightOperandNode.IsOk)
|
||||||
|
{
|
||||||
|
if (this.RightOperandNode.TigerType.Basetype == BaseType.Void)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary order operator with a non-valued right expression.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if (this.RightOperandNode.TigerType.Basetype != BaseType.Int &&
|
||||||
|
this.RightOperandNode.TigerType.Basetype != BaseType.String)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary order operator with a non-int or non-string right expression: '{0}' was found.",
|
||||||
|
this.RightOperandNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
bothOk = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bothOk && this.LeftOperandNode.TigerType.Basetype != this.RightOperandNode.TigerType.Basetype)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Invalid use of binary order operator with string and int expressions.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = bothOk ? TigerType.Int : TigerType.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.LeftOperandNode.GenerateCode(moduleBuilder);
|
||||||
|
this.RightOperandNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
if (this.LeftOperandNode.TigerType.Basetype == BaseType.String &&
|
||||||
|
this.RightOperandNode.TigerType.Basetype == BaseType.String)
|
||||||
|
{
|
||||||
|
MethodInfo compareString = ((Func<string, string, int>)String.Compare).Method;
|
||||||
|
|
||||||
|
ParameterExpression value = Expression.Parameter(typeof (int));
|
||||||
|
Expression compareCall = Expression.Call(compareString,
|
||||||
|
this.LeftOperandNode.VmExpression,
|
||||||
|
this.RightOperandNode.VmExpression);
|
||||||
|
|
||||||
|
Expression condition = null;
|
||||||
|
switch (this.ExpressionType)
|
||||||
|
{
|
||||||
|
case ExpressionType.GreaterThan:
|
||||||
|
condition = Expression.GreaterThan(value, Expression.Constant(0));
|
||||||
|
break;
|
||||||
|
case ExpressionType.GreaterThanOrEqual:
|
||||||
|
condition = Expression.GreaterThanOrEqual(value, Expression.Constant(0));
|
||||||
|
break;
|
||||||
|
case ExpressionType.LessThan:
|
||||||
|
condition = Expression.LessThan(value, Expression.Constant(0));
|
||||||
|
break;
|
||||||
|
case ExpressionType.LessThanOrEqual:
|
||||||
|
condition = Expression.LessThanOrEqual(value, Expression.Constant(0));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Expression block = Expression.Block(
|
||||||
|
new ParameterExpression[] {value},
|
||||||
|
new Expression[]
|
||||||
|
{
|
||||||
|
Expression.Assign(value, compareCall),
|
||||||
|
Expression.Condition(condition, Expression.Constant(1), Expression.Constant(0))
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
this.VmExpression = block;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Expression opHolds = Expression.MakeBinary(this.ExpressionType,
|
||||||
|
this.LeftOperandNode.VmExpression,
|
||||||
|
this.RightOperandNode.VmExpression);
|
||||||
|
|
||||||
|
this.VmExpression = Expression.Condition(opHolds, Expression.Constant(1), Expression.Constant(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
abstract class RelationalNode : BinaryNode
|
||||||
|
{
|
||||||
|
protected RelationalNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
base.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
this.VmExpression = Expression.Condition(this.VmExpression, Expression.Constant(1), Expression.Constant(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
54
YATC/ASTNodes/LocalNode/ExpressionNode/ExpressionNode.cs
Normal file
54
YATC/ASTNodes/LocalNode/ExpressionNode/ExpressionNode.cs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
public abstract class ExpressionNode : LocalNode
|
||||||
|
{
|
||||||
|
protected ExpressionNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private TigerType _tigerType = TigerType.Error;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tipo resultante de la expresión
|
||||||
|
/// </summary>
|
||||||
|
public TigerType TigerType
|
||||||
|
{
|
||||||
|
get { return _tigerType; }
|
||||||
|
protected set { _tigerType = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dice si un nodo está o no bien
|
||||||
|
/// </summary>
|
||||||
|
public bool IsOk { get { return !TigerType.Equals(TigerType.Error); } }
|
||||||
|
}
|
||||||
|
}
|
||||||
77
YATC/ASTNodes/LocalNode/ExpressionNode/NegNode.cs
Normal file
77
YATC/ASTNodes/LocalNode/ExpressionNode/NegNode.cs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Permite poner números negativos
|
||||||
|
/// </summary>
|
||||||
|
class NegNode : ExpressionNode
|
||||||
|
{
|
||||||
|
public NegNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ExpressionNode OperandNode { get { return (ExpressionNode)TigerChildren[0]; } }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.OperandNode.CheckSemantics(scope, report);
|
||||||
|
if (!this.OperandNode.IsOk)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (OperandNode.TigerType.Basetype == BaseType.Void)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
report.AddError(this.Line, this.Column, "Type mismatch: Invalid use of unary minus operator with a non-valued expression.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (OperandNode.TigerType.Basetype != BaseType.Int)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
report.AddError(this.Line, this.Column, "Type mismatch: Invalid use of unary minus operator with a non-int expression.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = TigerType.Int;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.OperandNode.GenerateCode(moduleBuilder);
|
||||||
|
this.VmExpression = Expression.Negate(this.OperandNode.VmExpression);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
154
YATC/ASTNodes/LocalNode/ExpressionNode/RecordInstNode.cs
Normal file
154
YATC/ASTNodes/LocalNode/ExpressionNode/RecordInstNode.cs
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class RecordInstNode : ExpressionNode
|
||||||
|
{
|
||||||
|
public RecordInstNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||||
|
public FieldInstNode[] FieldInstNodes
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return TigerChildren.Length > 1 ?
|
||||||
|
TigerChildren[1].TigerChildren.Cast<FieldInstNode>().ToArray() :
|
||||||
|
new FieldInstNode[] {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private RecordType _recordType;
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
TigerTypeInfo recordInfo = scope.FindTypeInfo(this.IdNode.Name, false);
|
||||||
|
if (recordInfo == null)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Undeclared record type: '{0}'.", this.IdNode.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recordInfo.Holder.TigerType.Basetype != BaseType.Record)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: given type is not a record: '{0}'.", this.IdNode.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_recordType = recordInfo.Holder.TigerType as RecordType;
|
||||||
|
if (_recordType == null)
|
||||||
|
throw new NullReferenceException();
|
||||||
|
|
||||||
|
if (_recordType.FieldInfos.Length != this.FieldInstNodes.Length)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Record fields length mismatch '{0}': expecting {1} and {2} found.",
|
||||||
|
this.IdNode.Name,
|
||||||
|
_recordType.FieldInfos.Length,
|
||||||
|
this.FieldInstNodes.Length);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < this.FieldInstNodes.Length; i++)
|
||||||
|
{
|
||||||
|
FieldInstNode field = this.FieldInstNodes[i];
|
||||||
|
|
||||||
|
if (field.IdNode.Name != _recordType.FieldInfos[i].Name)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Field name mismatch: field number {0} of type '{1}' should be called '{2}' instead of '{3}'.",
|
||||||
|
i.ToString(),
|
||||||
|
this.IdNode.Name,
|
||||||
|
_recordType.FieldInfos[i].Name,
|
||||||
|
field.IdNode.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
field.ExpressionNode.CheckSemantics(scope, report);
|
||||||
|
if (!field.ExpressionNode.IsOk)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!field.ExpressionNode.TigerType.IsAssignableTo(_recordType.FieldInfos[i].Holder.TigerType))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: field '{1}' of type '{0}' should be of type '{2}' instead of '{3}'",
|
||||||
|
this.IdNode.Name, field.IdNode.Name,
|
||||||
|
_recordType.FieldInfos[i].Holder.TigerType.Name, field.ExpressionNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = _recordType;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
NewExpression ctor = Expression.New(this.TigerType.GetCLRType());
|
||||||
|
ParameterExpression tmp = Expression.Parameter(this.TigerType.GetCLRType());
|
||||||
|
var fieldBindings = new MemberBinding[_recordType.FieldInfos.Length];
|
||||||
|
|
||||||
|
for (int i = 0; i < FieldInstNodes.Length; i++)
|
||||||
|
{
|
||||||
|
this.FieldInstNodes[i].ExpressionNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
fieldBindings[i] = Expression.Bind(
|
||||||
|
_recordType.GetCLRType().GetMember(_recordType.FieldInfos[i].Name)[0],
|
||||||
|
Expression.Convert(
|
||||||
|
this.FieldInstNodes[i].ExpressionNode.VmExpression,
|
||||||
|
_recordType.FieldInfos[i].Holder.TigerType.GetCLRType()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Expression initializer = Expression.MemberInit(ctor, fieldBindings);
|
||||||
|
|
||||||
|
var assign = Expression.Assign(tmp, initializer);
|
||||||
|
BlockExpression initBlockExpression = Expression.Block(
|
||||||
|
new ParameterExpression[] { tmp },
|
||||||
|
new Expression[] { assign, tmp });
|
||||||
|
|
||||||
|
this.VmExpression = initBlockExpression;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class AssignNode : StatementNode
|
||||||
|
{
|
||||||
|
public AssignNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public VarAccessNode LeftValueNode { get { return (VarAccessNode)TigerChildren[0]; } }
|
||||||
|
public ExpressionNode RightExpressionNode { get { return (ExpressionNode)TigerChildren[1]; } }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.LeftValueNode.CheckSemantics(scope, report);
|
||||||
|
this.RightExpressionNode.CheckSemantics(scope, report);
|
||||||
|
|
||||||
|
// No se reporta el error para evitar cascadas de errores
|
||||||
|
this.TigerType = this.LeftValueNode.IsOk && this.RightExpressionNode.IsOk
|
||||||
|
? TigerType.Void
|
||||||
|
: TigerType.Error;
|
||||||
|
|
||||||
|
if (!this.IsOk)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (this.RightExpressionNode.TigerType.Equals(TigerType.Void))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column, "Right hand side must evaluate to a returning value.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Assignment */
|
||||||
|
if (!this.RightExpressionNode.TigerType.IsAssignableTo(this.LeftValueNode.TigerType))
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Types of variable declaration and expression do not match: '{0}' and '{1}'",
|
||||||
|
this.LeftValueNode.TigerType.Name, this.RightExpressionNode.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// checks if left variable is not read-only, i.e., it is not defined in a ForNode
|
||||||
|
// la segunda pregunta accessNode == null es por si se quiere agregar campos o arrays
|
||||||
|
if (this.LeftValueNode.VariableInfo.IsReadOnly && this.LeftValueNode.AccessNode == null)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Cannot assign to a read-only variable (it may be declared within a for control structure).");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = TigerType.Void;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.LeftValueNode.GenerateCode(moduleBuilder);
|
||||||
|
this.RightExpressionNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
//int isOK = (int)(34 < 342);
|
||||||
|
|
||||||
|
this.VmExpression =
|
||||||
|
Expression.Block(
|
||||||
|
Expression.Assign(
|
||||||
|
this.LeftValueNode.VmExpression,
|
||||||
|
Expression.Convert(
|
||||||
|
this.RightExpressionNode.VmExpression,
|
||||||
|
this.LeftValueNode.TigerType.GetCLRType()
|
||||||
|
)
|
||||||
|
),
|
||||||
|
Expression.Empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class BreakNode : StatementNode
|
||||||
|
{
|
||||||
|
public BreakNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public IBreakeableNode Owner { get; set; }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
foreach (var node in this.GetNodesToRoot())
|
||||||
|
{
|
||||||
|
var exprSeq = node as ExprSeqNode;
|
||||||
|
if (exprSeq != null)
|
||||||
|
exprSeq.HasBreakInside = true;
|
||||||
|
|
||||||
|
var breakable = node as IBreakeableNode;
|
||||||
|
if (breakable != null)
|
||||||
|
{
|
||||||
|
this.Owner = breakable;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node is FunDeclNode)
|
||||||
|
{
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Break loop control structure not found within function.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.Owner == null)
|
||||||
|
{
|
||||||
|
report.AddError(Line, Column, "Break does not have a matching loop control structure owner.");
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = TigerType.Void;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
this.VmExpression = Expression.Break(this.Owner.BreakTarget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class EmptyNode : StatementNode
|
||||||
|
{
|
||||||
|
public EmptyNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Void;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
// vacío a propósito
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
127
YATC/ASTNodes/LocalNode/ExpressionNode/StatementNode/ForNode.cs
Normal file
127
YATC/ASTNodes/LocalNode/ExpressionNode/StatementNode/ForNode.cs
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// for id := expr_1 to expr_2 do expr_3;
|
||||||
|
/// </summary>
|
||||||
|
class ForNode : StatementNode, IBreakeableNode
|
||||||
|
{
|
||||||
|
public ForNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||||
|
public ExpressionNode FromExpression { get { return (ExpressionNode)TigerChildren[1]; } }
|
||||||
|
public ExpressionNode ToExpression { get { return (ExpressionNode)TigerChildren[2]; } }
|
||||||
|
public ExpressionNode DoExpression { get { return (ExpressionNode)TigerChildren[3]; } }
|
||||||
|
|
||||||
|
public LabelTarget BreakTarget { get; set; }
|
||||||
|
private VariableInfo _iterVarInfo;
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.FromExpression.CheckSemantics(scope, report);
|
||||||
|
this.ToExpression.CheckSemantics(scope, report);
|
||||||
|
|
||||||
|
TigerScope innerScope = scope.CreateChildScope();
|
||||||
|
|
||||||
|
if (innerScope.CanFindFunVarInfo(this.IdNode.Name, false))
|
||||||
|
report.AddWarning(this.Line, this.Column,
|
||||||
|
"Variable name hides outer scope variable or function: '{0}'.",
|
||||||
|
this.IdNode.Name);
|
||||||
|
|
||||||
|
_iterVarInfo = new VariableInfo(this.IdNode.Name, new TigerTypeHolder(TigerType.Int), false) { IsReadOnly = true };
|
||||||
|
innerScope.Add(_iterVarInfo);
|
||||||
|
|
||||||
|
this.DoExpression.CheckSemantics(innerScope, report);
|
||||||
|
|
||||||
|
if (!this.FromExpression.IsOk || !this.ToExpression.IsOk || !this.DoExpression.IsOk)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.FromExpression.TigerType.Basetype != BaseType.Int ||
|
||||||
|
this.ToExpression.TigerType.Basetype != BaseType.Int)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Expecting integer (or alias) type on intervals expression: '{0}' and '{1}' were found.",
|
||||||
|
this.FromExpression.TigerType.Name,
|
||||||
|
this.ToExpression.TigerType.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.DoExpression.TigerType.Basetype != BaseType.Void)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Expecting void return type in for expression: '{0}' was found.",
|
||||||
|
this.DoExpression.TigerType.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = TigerType.Void;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
BreakTarget = Expression.Label();
|
||||||
|
ParameterExpression iter = _iterVarInfo.ParameterExpression =
|
||||||
|
Expression.Parameter(typeof(int), "iter");
|
||||||
|
ParameterExpression fromExpr = Expression.Parameter(typeof(int), "fromExpr");
|
||||||
|
ParameterExpression toExpr = Expression.Parameter(typeof(int), "toExpr");
|
||||||
|
|
||||||
|
this.FromExpression.GenerateCode(moduleBuilder);
|
||||||
|
this.ToExpression.GenerateCode(moduleBuilder);
|
||||||
|
this.DoExpression.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
BlockExpression blockExpression = Expression.Block(
|
||||||
|
new ParameterExpression[] { iter, fromExpr, toExpr },
|
||||||
|
new Expression[]
|
||||||
|
{
|
||||||
|
Expression.Assign(fromExpr, this.FromExpression.VmExpression),
|
||||||
|
Expression.Assign(toExpr, this.ToExpression.VmExpression),
|
||||||
|
Expression.Assign(iter, fromExpr),
|
||||||
|
Expression.Loop(Expression.Block(
|
||||||
|
Expression.IfThen(Expression.GreaterThan(iter, toExpr),
|
||||||
|
Expression.Break(BreakTarget)),
|
||||||
|
this.DoExpression.VmExpression,
|
||||||
|
Expression.PostIncrementAssign(iter)
|
||||||
|
), BreakTarget)
|
||||||
|
});
|
||||||
|
|
||||||
|
this.VmExpression = blockExpression;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
interface IBreakeableNode
|
||||||
|
{
|
||||||
|
LabelTarget BreakTarget { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
abstract class StatementNode : ExpressionNode
|
||||||
|
{
|
||||||
|
protected StatementNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class WhileNode : StatementNode, IBreakeableNode
|
||||||
|
{
|
||||||
|
public WhileNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public LabelTarget BreakTarget { get; set; }
|
||||||
|
|
||||||
|
public ExpressionNode ConditionExpression { get { return (ExpressionNode)TigerChildren[0]; } }
|
||||||
|
|
||||||
|
public ExpressionNode DoExpression { get { return (ExpressionNode)TigerChildren[1]; } }
|
||||||
|
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.ConditionExpression.CheckSemantics(scope, report);
|
||||||
|
this.DoExpression.CheckSemantics(scope, report);
|
||||||
|
|
||||||
|
if (!this.ConditionExpression.IsOk || !this.DoExpression.IsOk)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.ConditionExpression.TigerType.Basetype != BaseType.Int)
|
||||||
|
{
|
||||||
|
report.AddError(Line, Column,
|
||||||
|
"Type mismatch: Expecting integer (or alias) type on condition expression: '{0}' was found.",
|
||||||
|
this.ConditionExpression.TigerType.Name);
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.DoExpression.TigerType.Basetype != BaseType.Void)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Error;
|
||||||
|
report.AddError(this.Line, this.Column,
|
||||||
|
"Type mismatch: Expecting void return type in while expression: '{0}' was found.",
|
||||||
|
this.DoExpression.TigerType.Name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.TigerType = TigerType.Void;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
BreakTarget = Expression.Label();
|
||||||
|
|
||||||
|
this.ConditionExpression.GenerateCode(moduleBuilder);
|
||||||
|
this.DoExpression.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
ConditionalExpression conditionalExpression = Expression.IfThenElse(
|
||||||
|
Expression.NotEqual(this.ConditionExpression.VmExpression, Expression.Constant(0)),
|
||||||
|
this.DoExpression.VmExpression,
|
||||||
|
Expression.Break(this.BreakTarget));
|
||||||
|
|
||||||
|
this.VmExpression = Expression.Loop(conditionalExpression, this.BreakTarget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
internal class FillInTypeNode : TypeNode
|
||||||
|
{
|
||||||
|
public FillInTypeNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.FillIn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
44
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/IdNode.cs
Normal file
44
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/IdNode.cs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class IdNode : TypeNode
|
||||||
|
{
|
||||||
|
public IdNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
// vacio exprofeso
|
||||||
|
this.TigerType = TigerType.Unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
69
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/TypeNode.cs
Normal file
69
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/TypeNode.cs
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class TypeNode : ExpressionNode
|
||||||
|
{
|
||||||
|
public TypeNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name { get { return Text; } }
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
switch (Text)
|
||||||
|
{
|
||||||
|
case "int":
|
||||||
|
TigerType = TigerType.Int;
|
||||||
|
break;
|
||||||
|
case "string":
|
||||||
|
TigerType = TigerType.String;
|
||||||
|
break;
|
||||||
|
case "nil":
|
||||||
|
TigerType = TigerType.Nil;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override void GenerateCode(ModuleBuilder moduleBuilder)
|
||||||
|
{
|
||||||
|
Type retType = this.TigerType.GetCLRType();
|
||||||
|
this.VmExpression = retType == null
|
||||||
|
? (Expression)Expression.Constant(null)
|
||||||
|
: Expression.Parameter(retType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
42
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/VoidNode.cs
Normal file
42
YATC/ASTNodes/LocalNode/ExpressionNode/TypeNode/VoidNode.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
internal class VoidNode : TypeNode
|
||||||
|
{
|
||||||
|
public VoidNode()
|
||||||
|
: base(null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CheckSemantics(TigerScope scope, Report report)
|
||||||
|
{
|
||||||
|
this.TigerType = TigerType.Void;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
46
YATC/ASTNodes/LocalNode/LocalNode.cs
Normal file
46
YATC/ASTNodes/LocalNode/LocalNode.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
public abstract class LocalNode : TigerNode
|
||||||
|
{
|
||||||
|
protected LocalNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void CheckSemantics(TigerScope scope, Report report);
|
||||||
|
|
||||||
|
internal abstract void GenerateCode(ModuleBuilder moduleBuilder);
|
||||||
|
|
||||||
|
public Expression VmExpression;
|
||||||
|
}
|
||||||
|
}
|
||||||
221
YATC/ASTNodes/ProgramNode.cs
Normal file
221
YATC/ASTNodes/ProgramNode.cs
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Reflection.Emit;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
public class ProgramNode : TigerNode
|
||||||
|
{
|
||||||
|
public ProgramNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
Report = new Report();
|
||||||
|
Scope = new TigerScope();
|
||||||
|
RecordNumber = 0;
|
||||||
|
AddStdLib();
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly Report Report;
|
||||||
|
internal readonly TigerScope Scope;
|
||||||
|
internal static int RecordNumber;
|
||||||
|
|
||||||
|
private FunctionInfo _print;
|
||||||
|
private FunctionInfo _printLine;
|
||||||
|
private FunctionInfo _printi;
|
||||||
|
private FunctionInfo _printiline;
|
||||||
|
private FunctionInfo _getline;
|
||||||
|
private FunctionInfo _ord;
|
||||||
|
private FunctionInfo _chr;
|
||||||
|
private FunctionInfo _exit;
|
||||||
|
private FunctionInfo _not;
|
||||||
|
private FunctionInfo _concat;
|
||||||
|
private FunctionInfo _substring;
|
||||||
|
private FunctionInfo _size;
|
||||||
|
|
||||||
|
internal ExpressionNode ExpressionNode { get { return (ExpressionNode)GetChild(0); } }
|
||||||
|
|
||||||
|
public void CheckSemantics()
|
||||||
|
{
|
||||||
|
if (this.ExpressionNode != null)
|
||||||
|
this.ExpressionNode.CheckSemantics(Scope, Report);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AssemblyBuilder GenerateCode(string name, string fileName, string outputDir)
|
||||||
|
{
|
||||||
|
AssemblyName assemblyName = new AssemblyName(name);
|
||||||
|
AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName,
|
||||||
|
AssemblyBuilderAccess.RunAndSave,
|
||||||
|
outputDir);
|
||||||
|
ModuleBuilder moduleBuilder = assembly.DefineDynamicModule(fileName, fileName);
|
||||||
|
TypeBuilder typeBuilder = moduleBuilder.DefineType("yatcProgram");
|
||||||
|
MethodBuilder mainMethod = typeBuilder.DefineMethod("main", MethodAttributes.Static);
|
||||||
|
|
||||||
|
Expression mainBlock;
|
||||||
|
if (this.ExpressionNode != null)
|
||||||
|
{
|
||||||
|
this.ExpressionNode.GenerateCode(moduleBuilder);
|
||||||
|
|
||||||
|
ParameterExpression outcode = Expression.Parameter(typeof(int));
|
||||||
|
ParameterExpression exception = Expression.Parameter(typeof(Exception));
|
||||||
|
|
||||||
|
MemberInfo excMessageMember = typeof(Exception).GetMember("Message")[0];
|
||||||
|
MemberInfo consoleError = typeof(Console).GetMember("Error")[0];
|
||||||
|
MethodInfo errorWrite = typeof(TextWriter).GetMethod("WriteLine", new Type[] { typeof(string) });
|
||||||
|
|
||||||
|
mainBlock = Expression.Block(
|
||||||
|
new ParameterExpression[] { outcode, exception },
|
||||||
|
new Expression[]
|
||||||
|
{
|
||||||
|
Expression.Assign(outcode, Expression.Constant(0)),
|
||||||
|
Expression.TryCatch(
|
||||||
|
Expression.Block(
|
||||||
|
this.ExpressionNode.VmExpression,
|
||||||
|
Expression.Empty()
|
||||||
|
),
|
||||||
|
new CatchBlock[]
|
||||||
|
{
|
||||||
|
Expression.MakeCatchBlock(
|
||||||
|
typeof(Exception),
|
||||||
|
exception,
|
||||||
|
Expression.Block(
|
||||||
|
Expression.Call(
|
||||||
|
Expression.MakeMemberAccess(null, consoleError),
|
||||||
|
errorWrite,
|
||||||
|
Expression.MakeMemberAccess(exception, excMessageMember)
|
||||||
|
),
|
||||||
|
Expression.Assign(outcode, Expression.Constant(1)),
|
||||||
|
Expression.Empty()
|
||||||
|
),
|
||||||
|
Expression.Constant(true)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
),
|
||||||
|
outcode
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mainBlock = Expression.Constant(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
LambdaExpression lambdaMainBlock = Expression.Lambda<Func<int>>(mainBlock);
|
||||||
|
lambdaMainBlock.CompileToMethod(mainMethod);
|
||||||
|
assembly.SetEntryPoint(mainMethod);
|
||||||
|
|
||||||
|
typeBuilder.CreateType();
|
||||||
|
moduleBuilder.CreateGlobalFunctions();
|
||||||
|
return assembly;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddStdLib()
|
||||||
|
{
|
||||||
|
var stringHolder = new TigerTypeHolder(TigerType.String);
|
||||||
|
var intHolder = new TigerTypeHolder(TigerType.Int);
|
||||||
|
var voidHolder = new TigerTypeHolder(TigerType.Void);
|
||||||
|
|
||||||
|
_print = new FunctionInfo("print", new[] { new VariableInfo("s", stringHolder, true), }, voidHolder, true);
|
||||||
|
_printLine = new FunctionInfo("printline", new[] { new VariableInfo("s", stringHolder, true), }, voidHolder, true);
|
||||||
|
_printi = new FunctionInfo("printi", new[] { new VariableInfo("i", intHolder, true), }, voidHolder, true);
|
||||||
|
_printiline = new FunctionInfo("printiline", new[] { new VariableInfo("i", intHolder, true), }, voidHolder, true);
|
||||||
|
_getline = new FunctionInfo("getline", new VariableInfo[0], stringHolder, true);
|
||||||
|
_ord = new FunctionInfo("ord", new[] { new VariableInfo("s", stringHolder, true), }, intHolder, true);
|
||||||
|
_chr = new FunctionInfo("chr", new[] { new VariableInfo("i", intHolder, true), }, stringHolder, true);
|
||||||
|
_size = new FunctionInfo("size", new[] { new VariableInfo("s", stringHolder, true), }, intHolder, true);
|
||||||
|
_substring = new FunctionInfo("substring", new[]
|
||||||
|
{
|
||||||
|
new VariableInfo("s", stringHolder, true),
|
||||||
|
new VariableInfo("f", intHolder, true),
|
||||||
|
new VariableInfo("n", intHolder, true),
|
||||||
|
},
|
||||||
|
stringHolder, true);
|
||||||
|
_concat = new FunctionInfo("concat",
|
||||||
|
new[]
|
||||||
|
{
|
||||||
|
new VariableInfo("s1", stringHolder, true),
|
||||||
|
new VariableInfo("s2", stringHolder, true),
|
||||||
|
},
|
||||||
|
stringHolder,
|
||||||
|
true);
|
||||||
|
_not = new FunctionInfo("not", new[] { new VariableInfo("i", intHolder, true), }, intHolder, true);
|
||||||
|
_exit = new FunctionInfo("exit", new[] { new VariableInfo("i", intHolder, true), }, voidHolder, true);
|
||||||
|
|
||||||
|
Scope.Add(_print);
|
||||||
|
Scope.Add(_printLine);
|
||||||
|
Scope.Add(_printi);
|
||||||
|
Scope.Add(_printiline);
|
||||||
|
Scope.Add(_getline);
|
||||||
|
Scope.Add(_ord);
|
||||||
|
Scope.Add(_chr);
|
||||||
|
Scope.Add(_size);
|
||||||
|
Scope.Add(_substring);
|
||||||
|
Scope.Add(_concat);
|
||||||
|
Scope.Add(_not);
|
||||||
|
Scope.Add(_exit);
|
||||||
|
|
||||||
|
Scope.Add(new TigerTypeInfo("string", new TigerTypeHolder(TigerType.String), true));
|
||||||
|
Scope.Add(new TigerTypeInfo("int", new TigerTypeHolder(TigerType.Int), true));
|
||||||
|
Scope.Add(new TigerTypeInfo("nil", new TigerTypeHolder(TigerType.Nil), true));
|
||||||
|
|
||||||
|
// althought it is defined and can be returned as a valid program expression it cannot be called by name
|
||||||
|
Scope.Add(new TigerTypeInfo("!void", new TigerTypeHolder(TigerType.Void), true));
|
||||||
|
|
||||||
|
MethodInfo writeString = ((Action<string>)Console.Write).Method;
|
||||||
|
MethodInfo writeLineString = ((Action<string>)Console.WriteLine).Method;
|
||||||
|
MethodInfo writeInt = ((Action<int>)Console.Write).Method;
|
||||||
|
MethodInfo writeLineInt = ((Action<int>)Console.WriteLine).Method;
|
||||||
|
MethodInfo readLine = ((Func<string>)Console.ReadLine).Method;
|
||||||
|
MethodInfo chr = ((Func<byte, string>)Convert.ToString).Method;
|
||||||
|
MethodInfo concat = ((Func<string, string, string>)string.Concat).Method;
|
||||||
|
|
||||||
|
_print.MethodInfo = writeString;
|
||||||
|
_printLine.MethodInfo = writeLineString;
|
||||||
|
_printi.MethodInfo = writeInt;
|
||||||
|
_printiline.MethodInfo = writeLineInt;
|
||||||
|
_getline.MethodInfo = readLine;
|
||||||
|
//_chr.MethodInfo = chr;
|
||||||
|
_concat.MethodInfo = concat;
|
||||||
|
|
||||||
|
Expression<Func<int, string>> lambdaChr = (b) => ( new string(Convert.ToChar(((byte)(b) > 127) ? int.MaxValue : (byte)b), 1));
|
||||||
|
Expression<Func<string, int>> lambdaOrd = (x) => (string.IsNullOrEmpty(x) ? -1 : Convert.ToByte(x[0]));
|
||||||
|
Expression<Func<string, int>> lambdaSize = (x) => (x.Length);
|
||||||
|
Expression<Func<string, int, int, string>> lambdaSubstring = (x, y, z) => (x.Substring(y, z));
|
||||||
|
Expression<Func<int, int>> lambdaNot = (x) => (x == 0 ? 1 : 0);
|
||||||
|
|
||||||
|
_ord.LambdaExpression = lambdaOrd;
|
||||||
|
_size.LambdaExpression = lambdaSize;
|
||||||
|
_substring.LambdaExpression = lambdaSubstring;
|
||||||
|
_not.LambdaExpression = lambdaNot;
|
||||||
|
_chr.LambdaExpression = lambdaChr;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
77
YATC/ASTNodes/TigerNode.cs
Normal file
77
YATC/ASTNodes/TigerNode.cs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using Antlr.Runtime.Tree;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
public abstract class TigerNode : CommonTree
|
||||||
|
{
|
||||||
|
protected TigerNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsRoot
|
||||||
|
{
|
||||||
|
get { return Parent == null; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Lists all the nodes from current to root
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<TigerNode> GetNodesToRoot()
|
||||||
|
{
|
||||||
|
if (this.IsRoot)
|
||||||
|
yield break;
|
||||||
|
|
||||||
|
Debug.Assert(this.Parent is TigerNode);
|
||||||
|
|
||||||
|
var parent = Parent as TigerNode;
|
||||||
|
yield return parent;
|
||||||
|
foreach (var item in parent.GetNodesToRoot())
|
||||||
|
yield return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Column { get { return CharPositionInLine; } }
|
||||||
|
|
||||||
|
//public new TigerNode Parent
|
||||||
|
//{
|
||||||
|
// get { return (TigerNode)base.Parent; }
|
||||||
|
//}
|
||||||
|
|
||||||
|
public TigerNode[] TigerChildren
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return base.Children == null ? new TigerNode[0] : base.Children.Cast<TigerNode>().ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
83
YATC/ASTNodes/TigerTreeAdaptor.cs
Normal file
83
YATC/ASTNodes/TigerTreeAdaptor.cs
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using Antlr.Runtime.Tree;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using YATC.Grammar;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
public class TigerTreeAdaptor : CommonTreeAdaptor
|
||||||
|
{
|
||||||
|
private readonly Dictionary<int, Type> _payloadCache;
|
||||||
|
|
||||||
|
public TigerTreeAdaptor()
|
||||||
|
{
|
||||||
|
_payloadCache = new Dictionary<int, Type>
|
||||||
|
{
|
||||||
|
{tigerParser.INTKEY, typeof (TypeNode)},
|
||||||
|
{tigerParser.STRINGKEY, typeof (TypeNode)},
|
||||||
|
{tigerParser.NILKEY, typeof (TypeNode)}
|
||||||
|
};
|
||||||
|
|
||||||
|
FieldInfo[] _fields = typeof(tigerParser).GetFields();
|
||||||
|
Assembly executingAssembly = Assembly.GetExecutingAssembly();
|
||||||
|
|
||||||
|
foreach (var field in _fields)
|
||||||
|
{
|
||||||
|
if (!field.IsStatic)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
string name = GetName(field.Name);
|
||||||
|
Type type = executingAssembly.GetType(name);
|
||||||
|
if (type != null)
|
||||||
|
_payloadCache[(int)field.GetRawConstantValue()] = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetName(string name)
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
foreach (var x in name.Split('_'))
|
||||||
|
sb.Append(char.ToUpper(x[0]) + x.Substring(1, x.Length - 1).ToLower());
|
||||||
|
return string.Format("YATC.ASTNodes.{0}Node", sb);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override object Create(IToken payload)
|
||||||
|
{
|
||||||
|
if (payload == null)
|
||||||
|
return new UnknownNode(null);
|
||||||
|
//return base.Create(null);
|
||||||
|
|
||||||
|
Type type;
|
||||||
|
bool foundType = _payloadCache.TryGetValue(payload.Type, out type);
|
||||||
|
return foundType ? Activator.CreateInstance(type, payload) : new UnknownNode(payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
40
YATC/ASTNodes/UtilNode/FieldInstNode.cs
Normal file
40
YATC/ASTNodes/UtilNode/FieldInstNode.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class FieldInstNode: UtilNode
|
||||||
|
{
|
||||||
|
public FieldInstNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public IdNode IdNode { get { return (IdNode) TigerChildren[0]; } }
|
||||||
|
public ExpressionNode ExpressionNode{ get { return (ExpressionNode) TigerChildren[1]; } }
|
||||||
|
}
|
||||||
|
}
|
||||||
45
YATC/ASTNodes/UtilNode/TypeFieldNode.cs
Normal file
45
YATC/ASTNodes/UtilNode/TypeFieldNode.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Declaracion de parametros de funciones y campos en la declaracion de un record.
|
||||||
|
/// </summary>
|
||||||
|
class TypeFieldNode : UtilNode
|
||||||
|
{
|
||||||
|
public TypeFieldNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TypeNode IdNode { get { return (TypeNode)TigerChildren[0]; } }
|
||||||
|
public TypeNode TypeNode { get { return (TypeNode)TigerChildren[1]; } }
|
||||||
|
|
||||||
|
public string Name { get { return this.IdNode.Name; } }
|
||||||
|
}
|
||||||
|
}
|
||||||
38
YATC/ASTNodes/UtilNode/UnknownNode.cs
Normal file
38
YATC/ASTNodes/UtilNode/UnknownNode.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
class UnknownNode : TigerNode
|
||||||
|
{
|
||||||
|
public UnknownNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
38
YATC/ASTNodes/UtilNode/UtilNode.cs
Normal file
38
YATC/ASTNodes/UtilNode/UtilNode.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using Antlr.Runtime;
|
||||||
|
using YATC.Scope;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
abstract class UtilNode : TigerNode
|
||||||
|
{
|
||||||
|
protected UtilNode(IToken payload)
|
||||||
|
: base(payload)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
46
YATC/Grammar/ParsingException.cs
Normal file
46
YATC/Grammar/ParsingException.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using Antlr.Runtime;
|
||||||
|
|
||||||
|
namespace YATC.Grammar
|
||||||
|
{
|
||||||
|
public class ParsingException : Exception
|
||||||
|
{
|
||||||
|
public ParsingException(string message, Exception innerException)
|
||||||
|
: base(message, innerException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public RecognitionException RecognitionError
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return InnerException as RecognitionException;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
YATC/Properties/AssemblyInfo.cs
Normal file
36
YATC/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// General Information about an assembly is controlled through the following
|
||||||
|
// set of attributes. Change these attribute values to modify the information
|
||||||
|
// associated with an assembly.
|
||||||
|
[assembly: AssemblyTitle("YATC")]
|
||||||
|
[assembly: AssemblyDescription("Yet Another Tiger Compiler code base")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("")]
|
||||||
|
[assembly: AssemblyProduct("YATC")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright Damian Valdes Santiago & Juan Carlos Pujol Mainegra © 2013-2014")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
// Setting ComVisible to false makes the types in this assembly not visible
|
||||||
|
// to COM components. If you need to access a type in this assembly from
|
||||||
|
// COM, set the ComVisible attribute to true on that type.
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||||
|
[assembly: Guid("791f0f8e-42cf-4906-8b7b-8f7b011f7758")]
|
||||||
|
|
||||||
|
// Version information for an assembly consists of the following four values:
|
||||||
|
//
|
||||||
|
// Major Version
|
||||||
|
// Minor Version
|
||||||
|
// Build Number
|
||||||
|
// Revision
|
||||||
|
//
|
||||||
|
// You can specify all the values or you can default the Build and Revision Numbers
|
||||||
|
// by using the '*' as shown below:
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
[assembly: AssemblyVersion("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||||
43
YATC/Scope/Report/Item.cs
Normal file
43
YATC/Scope/Report/Item.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
public class Item
|
||||||
|
{
|
||||||
|
public Item(Level level, int line, int column, string text)
|
||||||
|
{
|
||||||
|
Level = level;
|
||||||
|
Line = line;
|
||||||
|
Column = column;
|
||||||
|
Text = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly Level Level;
|
||||||
|
public readonly int Line;
|
||||||
|
public readonly int Column;
|
||||||
|
public readonly string Text;
|
||||||
|
}
|
||||||
|
}
|
||||||
79
YATC/Scope/Report/Report.cs
Normal file
79
YATC/Scope/Report/Report.cs
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace YATC.ASTNodes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Indica cuán mal es el error
|
||||||
|
/// </summary>
|
||||||
|
public enum Level { Info = 0, Warning = 1, Error = 2 }
|
||||||
|
|
||||||
|
public class Report : IEnumerable<Item>
|
||||||
|
{
|
||||||
|
protected readonly List<Item> Items = new List<Item>();
|
||||||
|
public Level Level { get; private set; }
|
||||||
|
public bool IsOK { get { return Level != Level.Error; } }
|
||||||
|
|
||||||
|
public void AddError(int line, int column, string text, params object[] modifiers)
|
||||||
|
{
|
||||||
|
if (Level < Level.Error)
|
||||||
|
Level = Level.Error;
|
||||||
|
Items.Add(new Item(Level.Error, line, column, string.Format(text, modifiers)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddWarning(int line, int column, string text, params object[] modifiers)
|
||||||
|
{
|
||||||
|
if (Level < Level.Warning)
|
||||||
|
Level = Level.Warning;
|
||||||
|
Items.Add(new Item(Level.Warning, line, column, string.Format(text, modifiers)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddInfo(int line, int column, string text, params object[] modifiers)
|
||||||
|
{
|
||||||
|
if (Level < Level.Info)
|
||||||
|
Level = Level.Info;
|
||||||
|
Items.Add(new Item(Level.Info, line, column, string.Format(text, modifiers)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Reset()
|
||||||
|
{
|
||||||
|
Level = Level.Info;
|
||||||
|
Items.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator<Item> GetEnumerator()
|
||||||
|
{
|
||||||
|
return Items.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return GetEnumerator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
31
YATC/Scope/TigerInfo/FunVarInfo.cs
Normal file
31
YATC/Scope/TigerInfo/FunVarInfo.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
public abstract class FunVarInfo : TigerTypeInfo
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
60
YATC/Scope/TigerInfo/FunctionInfo.cs
Normal file
60
YATC/Scope/TigerInfo/FunctionInfo.cs
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
public class FunctionInfo : FunVarInfo
|
||||||
|
{
|
||||||
|
public FunctionInfo(string name, VariableInfo[] parameterInfo, TigerTypeHolder returnTypeHolder, bool isStandard)
|
||||||
|
{
|
||||||
|
this.Name = name;
|
||||||
|
this.ParameterInfo = parameterInfo;
|
||||||
|
this.Holder = returnTypeHolder;
|
||||||
|
this.IsStandard = isStandard;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionInfo(string name, VariableInfo[] parameterInfo, TigerTypeHolder returnTypeHolder,
|
||||||
|
MethodInfo methodInfo)
|
||||||
|
:this(name, parameterInfo, returnTypeHolder, true)
|
||||||
|
{
|
||||||
|
this.MethodInfo = methodInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MethodInfo MethodInfo;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Listado de parámetros
|
||||||
|
/// </summary>
|
||||||
|
public readonly VariableInfo[] ParameterInfo;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Función en la máquina virtual correspondiente
|
||||||
|
/// </summary>
|
||||||
|
public Expression LambdaExpression;
|
||||||
|
}
|
||||||
|
}
|
||||||
48
YATC/Scope/TigerInfo/TigerInfo.cs
Normal file
48
YATC/Scope/TigerInfo/TigerInfo.cs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Describe la existencia de un símbolo en la tabla de símbolos
|
||||||
|
/// </summary>
|
||||||
|
public abstract class TigerInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Nombre del atributo
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tipo del atributo (si es una función indica tipo de retorno)
|
||||||
|
/// </summary>
|
||||||
|
public TigerTypeHolder Holder { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Indica si es del sistema
|
||||||
|
/// </summary>
|
||||||
|
public bool IsStandard;
|
||||||
|
}
|
||||||
|
}
|
||||||
45
YATC/Scope/TigerInfo/TigerTypeInfo.cs
Normal file
45
YATC/Scope/TigerInfo/TigerTypeInfo.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
public class TigerTypeInfo: TigerInfo
|
||||||
|
{
|
||||||
|
internal TigerTypeInfo()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TigerTypeInfo(string name, TigerTypeHolder holder, bool isStandard)
|
||||||
|
{
|
||||||
|
if (holder == null)
|
||||||
|
throw new ArgumentNullException("holder");
|
||||||
|
this.Name = name;
|
||||||
|
this.Holder = holder;
|
||||||
|
this.IsStandard = isStandard;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
55
YATC/Scope/TigerInfo/VariableInfo.cs
Normal file
55
YATC/Scope/TigerInfo/VariableInfo.cs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
public class VariableInfo : FunVarInfo
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Determina si el atributo es un paramétro o no
|
||||||
|
/// </summary>
|
||||||
|
public bool IsParameter { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether the variable cannot be a left value of an assignment
|
||||||
|
/// </summary>
|
||||||
|
public bool IsReadOnly { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Refleja una variable dentro del ejecutable para la generación de código
|
||||||
|
/// </summary>
|
||||||
|
public ParameterExpression ParameterExpression { get; set; }
|
||||||
|
|
||||||
|
public VariableInfo(string name, TigerTypeHolder holder, bool isParameter)
|
||||||
|
{
|
||||||
|
this.Name = name;
|
||||||
|
this.Holder = holder;
|
||||||
|
this.IsParameter = isParameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
169
YATC/Scope/TigerScope.cs
Normal file
169
YATC/Scope/TigerScope.cs
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Implementa la tabla de símbolos junto a TigerGlobal
|
||||||
|
/// </summary>
|
||||||
|
public class TigerScope
|
||||||
|
{
|
||||||
|
public TigerScope()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private TigerScope(TigerScope parent, int parentIndex)
|
||||||
|
{
|
||||||
|
Parent = parent;
|
||||||
|
ParentIndex = parentIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int _index;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ambito padre que contiene a este scope
|
||||||
|
/// </summary>
|
||||||
|
public readonly TigerScope Parent;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Permite ver variables del padre de 0 a ParentIndex
|
||||||
|
/// </summary>
|
||||||
|
public readonly int ParentIndex;
|
||||||
|
|
||||||
|
public bool IsRoot { get { return Parent == null; } }
|
||||||
|
|
||||||
|
private readonly List<TigerScope> _children = new List<TigerScope>();
|
||||||
|
private readonly HashSet<TigerTypeInfo> _typeInfos = new HashSet<TigerTypeInfo>();
|
||||||
|
private readonly HashSet<FunVarInfo> _funVarInfos = new HashSet<FunVarInfo>();
|
||||||
|
|
||||||
|
public TigerScope[] GetChildren()
|
||||||
|
{
|
||||||
|
return _children.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TigerScope CreateChildScope()
|
||||||
|
{
|
||||||
|
return new TigerScope(this, _index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Add(VariableInfo variableInfo)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("Added to variable scope: {0} of type {1}",
|
||||||
|
variableInfo.Name,
|
||||||
|
variableInfo.Holder.TigerType != null ? variableInfo.Holder.TigerType.Name : "null");
|
||||||
|
return _funVarInfos.Add(variableInfo) && ++_index != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Add(FunctionInfo functionInfo)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("Added to function scope: {0} of type {1}",
|
||||||
|
functionInfo.Name,
|
||||||
|
functionInfo.Holder.TigerType.Name);
|
||||||
|
return _funVarInfos.Add(functionInfo) && ++_index != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Add(TigerTypeInfo tigerTypeInfo)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("Added to type scope: {0} of type {1}",
|
||||||
|
tigerTypeInfo.Name,
|
||||||
|
tigerTypeInfo.Holder.TigerType != null ? tigerTypeInfo.Holder.TigerType.Name: "null");
|
||||||
|
return _typeInfos.Add(tigerTypeInfo) && ++_index != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ContainsType(TigerType tigerType, bool localSearchOnly)
|
||||||
|
{
|
||||||
|
bool isLocal = _typeInfos.Any(x => x.Holder.TigerType.Equals(tigerType));
|
||||||
|
return isLocal || (!localSearchOnly && !IsRoot && Parent.ContainsType(tigerType, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
public VariableInfo[] GetLocalVariableInfos()
|
||||||
|
{
|
||||||
|
return _funVarInfos.Where(x => x is VariableInfo).Cast<VariableInfo>().ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public VariableInfo FindVariableInfo(string name, bool localSearchOnly)
|
||||||
|
{
|
||||||
|
var result = _funVarInfos.FirstOrDefault(x => x.Name == name);
|
||||||
|
if (result is FunctionInfo)
|
||||||
|
return null;
|
||||||
|
return (VariableInfo)result ?? (!localSearchOnly && !IsRoot ? Parent.FindVariableInfo(name, false) : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CanFindVariableInfo(string name, bool localSearchOnly)
|
||||||
|
{
|
||||||
|
return FindVariableInfo(name, localSearchOnly) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TigerTypeInfo[] GetLocalTypeInfos()
|
||||||
|
{
|
||||||
|
return _typeInfos.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public TigerTypeInfo FindTypeInfo(string name, bool localSearchOnly)
|
||||||
|
{
|
||||||
|
var result = _typeInfos.FirstOrDefault(x => x.Name == name);
|
||||||
|
return result ?? (!localSearchOnly && !IsRoot ? Parent.FindTypeInfo(name, false) : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CanFindTypeInfo(string name, bool localSearchOnly)
|
||||||
|
{
|
||||||
|
return FindTypeInfo(name, localSearchOnly) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionInfo[] GetFunctionInfos()
|
||||||
|
{
|
||||||
|
return _funVarInfos.Where(x => x is FunctionInfo).Cast<FunctionInfo>().ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunctionInfo FindFunctionInfo(string name, bool localSearchOnly)
|
||||||
|
{
|
||||||
|
FunVarInfo result = _funVarInfos.FirstOrDefault(x => x.Name == name);
|
||||||
|
if (result is VariableInfo)
|
||||||
|
return null;
|
||||||
|
return (FunctionInfo)result ?? (!localSearchOnly && !IsRoot ? Parent.FindFunctionInfo(name, false) : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CanFindFunctionInfo(string name, bool localSearchOnly)
|
||||||
|
{
|
||||||
|
return FindFunctionInfo(name, localSearchOnly) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FunVarInfo FindFunVarInfo(string name, bool localSearchOnly)
|
||||||
|
{
|
||||||
|
FunVarInfo result = _funVarInfos.FirstOrDefault(x => x.Name == name);
|
||||||
|
return result ?? (!localSearchOnly && !IsRoot ? Parent.FindFunctionInfo(name, false) : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CanFindFunVarInfo(string name, bool localSearchOnly)
|
||||||
|
{
|
||||||
|
return FindFunVarInfo(name, localSearchOnly) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
38
YATC/Scope/TigerType/ArrayType.cs
Normal file
38
YATC/Scope/TigerType/ArrayType.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
public class ArrayType: TigerType
|
||||||
|
{
|
||||||
|
public ArrayType(string name, TigerTypeHolder elementTypeHolder)
|
||||||
|
: base(name, BaseType.Array)
|
||||||
|
{
|
||||||
|
this.ElementTypeHolder = elementTypeHolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TigerTypeHolder ElementTypeHolder { get; internal set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
38
YATC/Scope/TigerType/RecordType.cs
Normal file
38
YATC/Scope/TigerType/RecordType.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
public class RecordType: TigerType
|
||||||
|
{
|
||||||
|
public RecordType(string name, VariableInfo[] fieldInfos)
|
||||||
|
: base(name, BaseType.Record)
|
||||||
|
{
|
||||||
|
this.FieldInfos = fieldInfos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public VariableInfo[] FieldInfos { get; internal set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
105
YATC/Scope/TigerType/TigerType.cs
Normal file
105
YATC/Scope/TigerType/TigerType.cs
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
public enum BaseType
|
||||||
|
{
|
||||||
|
Error = -1,
|
||||||
|
Int = 0,
|
||||||
|
String = 1,
|
||||||
|
Nil = 2,
|
||||||
|
Record = 3,
|
||||||
|
Array = 4,
|
||||||
|
Void = 5,
|
||||||
|
FillIn = 6,
|
||||||
|
Unknown = 7
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TigerType
|
||||||
|
{
|
||||||
|
public string Name { get; protected set; }
|
||||||
|
public BaseType Basetype { get; private set; }
|
||||||
|
|
||||||
|
public Type CLRType { get; set; }
|
||||||
|
|
||||||
|
protected TigerType(string name, BaseType basetype)
|
||||||
|
{
|
||||||
|
this.Name = name;
|
||||||
|
this.Basetype = basetype;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected TigerType(BaseType baseType)
|
||||||
|
: this(baseType.ToString().ToLowerInvariant(), baseType)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual Type GetCLRType()
|
||||||
|
{
|
||||||
|
if (CLRType != null)
|
||||||
|
return CLRType;
|
||||||
|
|
||||||
|
switch (Basetype)
|
||||||
|
{
|
||||||
|
case BaseType.Int:
|
||||||
|
return typeof(int);
|
||||||
|
case BaseType.String:
|
||||||
|
return typeof(string);
|
||||||
|
case BaseType.Void:
|
||||||
|
return typeof(void);
|
||||||
|
case BaseType.Nil:
|
||||||
|
return null;
|
||||||
|
case BaseType.Array:
|
||||||
|
return (this as ArrayType).ElementTypeHolder.TigerType.GetCLRType().MakeArrayType();
|
||||||
|
default:
|
||||||
|
throw new IndexOutOfRangeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly TigerType Error = new TigerType(BaseType.Error);
|
||||||
|
public static readonly TigerType Int = new TigerType(BaseType.Int);
|
||||||
|
public static readonly TigerType String = new TigerType(BaseType.String);
|
||||||
|
public static readonly TigerType Nil = new TigerType(BaseType.Nil);
|
||||||
|
public static readonly TigerType Void = new TigerType(BaseType.Void);
|
||||||
|
public static readonly TigerType FillIn = new TigerType(BaseType.FillIn);
|
||||||
|
public static readonly TigerType Unknown = new TigerType(BaseType.Unknown);
|
||||||
|
|
||||||
|
public virtual bool IsAssignableTo(TigerType other)
|
||||||
|
{
|
||||||
|
bool result = this.Equals(other) ||
|
||||||
|
(this.Basetype != BaseType.Record &&
|
||||||
|
this.Basetype != BaseType.Array && this.Basetype == other.Basetype) ||
|
||||||
|
(this.Basetype == BaseType.Nil && other.Basetype == BaseType.Array) ||
|
||||||
|
(this.Basetype == BaseType.Nil && other.Basetype == BaseType.Record) ||
|
||||||
|
(this.Basetype == BaseType.Nil && other.Basetype == BaseType.String);
|
||||||
|
Debug.WriteLine("Typesystem ruled that '{0}' {2} assignable to '{1}'.",
|
||||||
|
this.Name, other.Name, result ? "is" : "is NOT");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
YATC/Scope/TigerType/TigerTypeHolder.cs
Normal file
41
YATC/Scope/TigerType/TigerTypeHolder.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Yet Another Tiger Compiler (YATC)
|
||||||
|
*
|
||||||
|
* Copyright 2014 Damian Valdés Santiago, Juan Carlos Pujol Mainegra
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace YATC.Scope
|
||||||
|
{
|
||||||
|
public class TigerTypeHolder
|
||||||
|
{
|
||||||
|
public TigerTypeHolder()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TigerTypeHolder(TigerType tigerType)
|
||||||
|
{
|
||||||
|
this.TigerType = tigerType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TigerType TigerType;
|
||||||
|
}
|
||||||
|
}
|
||||||
145
YATC/YATC.csproj
Normal file
145
YATC/YATC.csproj
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProjectGuid>{D62DA087-4724-49A3-91B5-83689F442747}</ProjectGuid>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>YATC</RootNamespace>
|
||||||
|
<AssemblyName>YATC.Base</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<TargetFrameworkProfile />
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="Antlr3.Runtime">
|
||||||
|
<HintPath>..\Antlr3.Runtime.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="..\Grammar\tigerLexer.cs">
|
||||||
|
<Link>Grammar\tigerLexer.cs</Link>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="..\Grammar\tigerParser.cs">
|
||||||
|
<Link>Grammar\tigerParser.cs</Link>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="ASTNodes\LocalNode\DeclarationNode\FunDeclNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\DeclarationNode\DeclarationNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\DeclarationNode\FunDeclSeqNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\DeclarationNode\Node.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\DeclarationNode\TypeDeclSeqNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\DeclarationNode\AliasDeclNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\ArrayInstNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\AccessNode\AccessNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\AccessNode\ArrayAccess.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\AccessNode\FieldAccessNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\AtomicNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\ExprSeqNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\FunCallNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\IntNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\LetNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\NilNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\VarAccessNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\StringNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\ArithmeticNode\ArithmeticNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\ArithmeticNode\DivNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\ArithmeticNode\MinusNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\ArithmeticNode\MultNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\ArithmeticNode\PlusNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\BinaryNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\LogicNode\AndNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\LogicNode\LogicNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\LogicNode\OrNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\IdentityNode\EqNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\IdentityNode\IdentityNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\IdentityNode\NotEqNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\OrderNode\GtEqNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\OrderNode\GtNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\OrderNode\LtEqNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\OrderNode\LtNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\OrderNode\OrderNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\BinaryNode\RelationalNode\RelationalNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\ExpressionNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\RecordInstNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\NegNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\LocalNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\AssignNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\BreakNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\EmptyNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\ForNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\IBreakebleNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\AtomicNode\IfNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\StatementNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\DeclarationNode\TypeDeclNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\DeclarationNode\VarDeclNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\StatementNode\WhileNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\ProgramNode.cs" />
|
||||||
|
<Compile Include="Scope\Report\Item.cs" />
|
||||||
|
<Compile Include="Scope\Report\Report.cs" />
|
||||||
|
<Compile Include="Scope\TigerInfo\FunVarInfo.cs" />
|
||||||
|
<Compile Include="Scope\TigerType\TigerTypeHolder.cs" />
|
||||||
|
<Compile Include="Scope\TigerType\ArrayType.cs" />
|
||||||
|
<Compile Include="Scope\TigerType\RecordType.cs" />
|
||||||
|
<Compile Include="Scope\TigerType\TigerType.cs" />
|
||||||
|
<Compile Include="ASTNodes\UtilNode\FieldInstNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\DeclarationNode\ArrayDeclNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\TypeNode\FillInTypeNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\TypeNode\IdNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\UtilNode\TypeFieldNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\DeclarationNode\RecordDeclNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\TypeNode\TypeNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\LocalNode\ExpressionNode\TypeNode\VoidNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\UtilNode\UnknownNode.cs" />
|
||||||
|
<Compile Include="ASTNodes\UtilNode\UtilNode.cs" />
|
||||||
|
<Compile Include="Grammar\ParsingException.cs" />
|
||||||
|
<Compile Include="Scope\TigerInfo\TigerInfo.cs" />
|
||||||
|
<Compile Include="ASTNodes\TigerNode.cs" />
|
||||||
|
<Compile Include="Scope\TigerInfo\TigerTypeInfo.cs" />
|
||||||
|
<Compile Include="Scope\TigerScope.cs" />
|
||||||
|
<Compile Include="Scope\TigerInfo\FunctionInfo.cs" />
|
||||||
|
<Compile Include="Scope\TigerInfo\VariableInfo.cs" />
|
||||||
|
<Compile Include="ASTNodes\TigerTreeAdaptor.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Antlr3 Include="..\Grammar\tiger.g">
|
||||||
|
<Link>Grammar\tiger.g</Link>
|
||||||
|
<Generator>MSBuild:Compile</Generator>
|
||||||
|
<CustomToolNamespace>YATC.Grammar</CustomToolNamespace>
|
||||||
|
</Antlr3>
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
<Target Name="BeforeBuild">
|
||||||
|
</Target>
|
||||||
|
<Target Name="AfterBuild">
|
||||||
|
</Target>
|
||||||
|
-->
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user