finished lab6 i hope

This commit is contained in:
RustyRaptor committed 2021-04-01 19:50:19 -06:00
1 parent 887235d6a8
commit 8470e6b5ad
5 files changed
+159 -89

No files matched your search

+2 -1
View File
@@ -65,7 +65,7 @@ void ASTprint(int level, ASTnode *p)
if (p == NULL)
return;
else {
PT(level); /*indent */
PT(level); /* indent */
switch (p->type) {
case A_VAR_RVALUE:
printf("Variable %s \n", p->name);
@@ -218,6 +218,7 @@ void ASTprint(int level, ASTnode *p)
printf(" !");
break;
case A_UMINUS:
// print words so we know it's not just minus
printf(" Unary Minus");
break;
default:
+3
View File
@@ -16,6 +16,7 @@ static int mydebug;
/* define the enumerated types for the AST. THis is used to tell us what
sort of production rule we came across */
/* added all the needed tree nodes */
enum AST_Tree_Element_Type {
A_PROGRAM,
A_EXTERN,
@@ -47,6 +48,8 @@ enum AST_Tree_Element_Type {
// missing
};
/* Added all the needed operators */
enum AST_Operators {
A_PLUS,
A_MINUS,
+16 -12
View File
@@ -40,6 +40,13 @@ from LEX and how the operators are associated */
enum AST_Operators operator;
}
// I commented out the tokens that aren't being used.
// I also removed the token for T_LT because idk why one was a token and the
// other was a char??
// Added a token for each of the lex tokens
%token T_AND
%token T_ASSIGN
@@ -229,6 +236,10 @@ Statements:
$$->next = $2;
};
/* Moved all of the Statement into one rule and moved each item into it's own
separate rule.
*/
/* Connected these all */
Statement:
Block { $$ = $1; }
| Assign ';' { $$ = $1; }
@@ -240,6 +251,7 @@ Statement:
| ContinueStmt { $$ = $1; }
;
/* Created a continue statement idk if this was needed */
ContinueStmt: T_CONTINUE ';' {$$ = ASTCreateNode(A_CONTINUE);};
WhileStmt:
@@ -291,6 +303,9 @@ Assign:
Lvalue:
T_ID {
/* I am using A_VAR_RVALUE for both the LVALUE and RVALUE
Mostly because the node serves the same purpose.
*/
$$ = ASTCreateNode(A_VAR_RVALUE);
$$->name = $1;
}
@@ -379,18 +394,6 @@ Term:
$$->S2 = $3;
};
/* added modulus as a multi operator */
/*
A_PLUS,
A_MINUS,
A_TIMES,
A_DIVIDE,
A_MOD,
A_AND,
A_OR,
A_LEFTSHIFT,
A_RIGHTSHIFT,
A_NOT
*/
Multop:
'*' { $$ = A_TIMES; }
| '/' { $$ = A_DIVIDE; }
@@ -419,6 +422,7 @@ Factor:
$$->operator = A_NOT;
$$->S1 = $2;
}
/* moved this around a lot until I got it working in here */
| T_STRINGCONSTANT {
$$ = ASTCreateNode(A_CONSTANT_STRING);
$$->name = $1;
+32
View File
@@ -0,0 +1,32 @@
extern func print_int(int) bool;
extern func print_string(string) bool;
package lab6 {
var x[100] int;
var y int;
var y bool = true;
func main() int
{ var x int ;
var y int ;
while (x+ 5/2 <= 2 + z - 5)
{
if ( h - 2 >= 3 - 2)
{ read (x[100]);
}
else
{
write (x[100] + 200);
} // of else
f(3+x[x[100]], bar+200, 20);
return ;
return (x+5+7);
if ( x+10 > 10*20 )
{ x = x!=10;}
write (! ( (3 && 5) || (true && ! false)));
}
} // of main
func f(x int, y bool) void
{
} // of f
} // of package lab6
+106 -76
View File
@@ -2,86 +2,116 @@ Parsing completed
EXTERN FUNC print_int
EXTERN Type INT
END EXTERN with Type:
INT Package : foo
Variable x with type INT = 3
Variable Y with type BOOLEAN = 1
Variable A[100] with type INT
METHOD FUNCTION 'foo' with type INT
BOOLEAN EXTERN FUNC print_string
EXTERN Type STRING
END EXTERN with Type:
BOOLEAN Package : lab6
Variable x[100] with type INT
Variable y with type INT
Variable y with type BOOLEAN = 1
METHOD FUNCTION 'main' with type INT
(NONE)
BLOCK STATEMENT
Variable x with type INT
Variable y with type INT
WHILE STATEMENT
EXPR <=
EXPR +
Variable x
EXPR /
CONSTANT INTEGER 5
CONSTANT INTEGER 2
EXPR -
EXPR +
CONSTANT INTEGER 2
Variable z
CONSTANT INTEGER 5
BLOCK STATEMENT
IF STATEMENT
EXPR >=
EXPR -
Variable h
CONSTANT INTEGER 2
EXPR -
CONSTANT INTEGER 3
CONSTANT INTEGER 2
BLOCK STATEMENT
METHOD CALL name: read
(
METHOD ARG
Variable x
[
CONSTANT INTEGER 100
]
)
ELSE
BLOCK STATEMENT
METHOD CALL name: write
(
METHOD ARG
EXPR +
Variable x
[
CONSTANT INTEGER 100
]
CONSTANT INTEGER 200
)
METHOD CALL name: f
(
METHOD ARG
EXPR +
CONSTANT INTEGER 3
Variable x
[
Variable x
[
CONSTANT INTEGER 100
]
]
EXPR +
Variable bar
CONSTANT INTEGER 200
CONSTANT INTEGER 20
)
RETURN STATEMENT
RETURN STATEMENT
EXPR +
EXPR +
Variable x
CONSTANT INTEGER 5
CONSTANT INTEGER 7
IF STATEMENT
EXPR >
EXPR +
Variable x
CONSTANT INTEGER 10
EXPR *
CONSTANT INTEGER 10
CONSTANT INTEGER 20
BLOCK STATEMENT
ASSIGNMENT STATEMENT
Variable x
EXPR !=
Variable x
CONSTANT INTEGER 10
METHOD CALL name: write
(
METHOD ARG
EXPR !
EXPR ||
EXPR &&
CONSTANT INTEGER 3
CONSTANT INTEGER 5
EXPR &&
CONSTANT BOOLEAN true
EXPR !
CONSTANT BOOLEAN false
)
METHOD FUNCTION 'f' with type VOID
(
Method Variable x INT
Method Variable y BOOLEAN
)
BLOCK STATEMENT
Variable x with type INT
Variable A[100] with type BOOLEAN
BREAK STATEMENT
ASSIGNMENT STATEMENT
Variable x
EXPR +
Variable y
Variable z
[
Variable A
]
ASSIGNMENT STATEMENT
Variable x
CONSTANT BOOLEAN true
ASSIGNMENT STATEMENT
Variable y
CONSTANT BOOLEAN false
IF STATEMENT
EXPR >
Variable x
Variable y
BLOCK STATEMENT
ASSIGNMENT STATEMENT
Variable x
EXPR +
CONSTANT INTEGER 3
CONSTANT INTEGER 4
ELSE
BLOCK STATEMENT
ASSIGNMENT STATEMENT
Variable x
Variable y
RETURN STATEMENT
RETURN STATEMENT
RETURN STATEMENT
EXPR >>
EXPR <<
Variable x
Variable y
Variable z
WHILE STATEMENT
EXPR !=
CONSTANT BOOLEAN true
CONSTANT BOOLEAN false
BLOCK STATEMENT
ASSIGNMENT STATEMENT
Variable x
EXPR &&
CONSTANT INTEGER 3
CONSTANT INTEGER 2
METHOD CALL name: print_string
(
METHOD ARG
CONSTANT STRING "hello world"
)
ASSIGNMENT STATEMENT
Variable x
METHOD CALL name: f
(
METHOD ARG
METHOD CALL name: g
(
METHOD ARG
EXPR !
EXPR ||
EXPR &&
Variable x
Variable y
Variable z
)
)
Finished printing AST