-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.java
More file actions
101 lines (88 loc) · 2.98 KB
/
Program.java
File metadata and controls
101 lines (88 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
public class Program
{
public class FuncDef
{
public type.Field[] parameters;
public type.Type returntype;
public ast.Tree body;
public FuncDef( type.Field[] parameters,
type.Type returntype,
ast.Tree body )
{
this. parameters = parameters;
this. returntype = returntype;
this. body = body;
}
}
public java.util.HashMap< java.lang.String, ast.Tree > constdefs;
public type.StructStore structdefs;
public java.util.HashMap< java.lang.String, FuncDef > funcdefs;
public Program( )
{
constdefs = new java.util.HashMap<> ( );
structdefs = new type.StructStore( );
funcdefs = new java.util.HashMap<> ( );
}
public void addconstant( java.lang.String id, ast.Tree val )
{
if( constdefs. get( id ) != null )
throw new SemanticError( "constant " + id + " redefined " );
else
constdefs. put( id, val );
}
public void addstruct( java.lang.String id,
java.util.ArrayList< type.Field > fields )
{
if( structdefs. contains( id ))
throw new SemanticError( "struct " + id + " redefined " );
else
structdefs. insert( id, fields. toArray( new type.Field[] {} ));
}
public void addfunction( java.lang.String id,
java.util.ArrayList< type. Field > parameters,
type.Type returntype,
ast.Tree body )
{
if( funcdefs. get( id ) != null )
throw new SemanticError( "function " + id + " redefined" );
else
funcdefs. put( id, new FuncDef(
parameters. toArray( new type.Field[] {} ),
returntype, body ));
}
public java.lang.String toString( )
{
java.lang.StringBuilder res = new java.lang.StringBuilder( );
res. append( "Constants:\n" );
for( java.util.Map.Entry< java.lang.String, ast.Tree >
c : constdefs. entrySet( ))
{
res. append( " " );
res. append( c. getKey( ));
res. append( " = " );
res. append( c. getValue( ));
}
res. append( "\n" );
res. append( structdefs. toString( ));
res. append( "\n" );
res. append( "Functions\n" );
for( java.util.Map.Entry< java.lang.String, FuncDef >
f : funcdefs. entrySet( ))
{
res. append( " " );
res. append( f. getKey( ));
res. append( "( " );
for( int i = 0; i != f. getValue( ). parameters. length; ++ i )
{
if( i != 0 ) res. append( ", " );
res. append( f. getValue( ). parameters[i] );
}
res. append( " ): " );
res. append( f. getValue( ). returntype );
res. append( "\n" );
res. append( f. getValue( ). body. toString( 2 ));
res. append( "\n" );
}
return res. toString( );
}
}