Skip to content

Commit

Permalink
completed stage 6
Browse files Browse the repository at this point in the history
  • Loading branch information
clifordjoshy committed Feb 3, 2022
1 parent 983cd6a commit 0bc8a84
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Stage6/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ main = do
inputFile <- openFile (head args) ReadMode
fileContents <- hGetContents inputFile
let (tTable, gTable, sp, fDecl, main) = parseTokens (scanTokens fileContents)
print gTable
print tTable
-- print gTable
-- print tTable
-- print fDecl
let (_, mainAst) = main in putStr $ prettyPrint mainAst
-- let (_, mainAst) = main in putStr $ prettyPrint mainAst
let code = codeGen gTable sp tTable fDecl main
createOutputFile code
hClose inputFile
4 changes: 2 additions & 2 deletions Stage6/ParserState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ dotSymCheck symName dots = do

intCheck :: SyntaxTree -> State ParserState SyntaxTree
intCheck n = do
(_, _, _, symTab) <- get
if isInteger symTab n then return n else error $ "Integer value was expected on right side of assignment: " ++ show n
(tt, _, _, symTab) <- get
if isInteger symTab tt n then return n else error $ "Integer value was expected: " ++ show n

symCheck :: (Symbol -> Bool) -> String -> State ParserState ()
symCheck isSym n = do
Expand Down
15 changes: 7 additions & 8 deletions Stage6/SyntaxTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ data SyntaxTree
deriving (Show)

-- | Returns if a given constructor evaluates to an integer value
isInteger :: GSymbolTable -> SyntaxTree -> Bool
isInteger st (LeafVar var Deref) = getSymbolType (st Map.! var) == "int*"
isInteger st (LeafVar var _) = getSymbolType (st Map.! var) == "int"
isInteger st (LeafFn name _) = getSymbolType (st Map.! name) == "int"
isInteger _ LeafValInt {} = True
isInteger _ NodeArmc {} = True
isInteger _ _ = False
isInteger :: GSymbolTable -> TypeTable -> SyntaxTree -> Bool
isInteger st tt v@(LeafVar _ _) = getVarType st tt v == "int"
isInteger st _ f@(LeafFn _ _) = getFnType st f == "int"
isInteger _ _ LeafValInt {} = True
isInteger _ _ NodeArmc {} = True
isInteger _ _ _ = False

-- Used in ParserState. Takes merged sym table
-- Used in ParserState. Takes merged sym table. (check lvalue)
getVarType :: GSymbolTable -> TypeTable -> SyntaxTree -> String
getVarType st _ (LeafVar var Deref) = init $ getSymbolType (st Map.! var) -- Remove "*" from the end
getVarType st tt (LeafVar var (Dot dotList)) = resolveDotType tt (getSymbolType (st Map.! var)) dotList
Expand Down
54 changes: 54 additions & 0 deletions Stage6/Test/arrptr.expl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
type
dataT {
int a;
dataT b;
}
endtype

decl
dataT arr[5], func(dataT *arg), h;
enddecl

dataT func(dataT *arg)
{
begin
*arg = alloc();
h = *arg;
h.a = 8;
h.b = h;
return null;
end
}

int main()
{
decl
dataT *ptr, px;
enddecl

begin
initialize();
arr[0] = alloc();
arr[1] = alloc();
arr[2] = alloc();
arr[3] = alloc();
arr[4] = alloc();

ptr = &arr[1];
px = *ptr;
px.a = 5;
write(px.a);
write(func(ptr));
px = arr[1];
write(px.b.b.b.b.b.a);
arr[0] = *ptr;
if(arr[0] == arr[1]) then
write("equality works");
endif;
if(arr[0] == "no") then
write ("equality no work");
endif;

return 0;
end
}
107 changes: 107 additions & 0 deletions Stage6/Test/bst.expl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
type
bst{
int a;
bst left;
bst right;
}
endtype

decl
int in,opt;
bst insert(bst h, int key);
int inOrder(bst h);
int preOrder(bst h);
int postOrder(bst h);
enddecl

bst insert(bst h, int key)
{

begin
if (h == null) then
h = alloc();
h.a = key;
h.left = null;
h.right = null;

else if (key < h.a) then
h.left = insert(h.left, key);

else if (key > h.a) then
h.right = insert(h.right, key);
endif;
endif;
endif;

return h;
end
}

int inOrder(bst h){

begin

if(h!=null) then

in=inOrder(h.left);
write(h.a);
in=inOrder(h.right);
endif;
return 1;
end
}

int preOrder(bst h){

begin

if(h!=null) then
write(h.a);
in=preOrder(h.left);

in=preOrder(h.right);
endif;
return 1;
end
}

int postOrder(bst h){

begin

if(h!=null) then

in=postOrder(h.left);

in=postOrder(h.right);
write(h.a);
endif;
return 1;
end
}

int main()
{
decl
int val,flag;
bst Root;
enddecl

begin
val = initialize();
Root = null;

read(val);

while(val!=0) do
Root = insert(Root,val);
read(val);
endwhile;

in = inOrder(Root);
in = preOrder(Root);
in = postOrder(Root);

return 9;
end
}
57 changes: 57 additions & 0 deletions Stage6/Test/euclid2.expl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
type
node
{
int d;
int s;
int t;
}
endtype

decl
node y,z,gcd(int a,int b);
enddecl

node gcd(int a,int b)
{
decl
int q,r,temp;
enddecl

begin
if(b==0) then
y.d = a;
y.s = 1;
y.t = 0;
else
q = a/b;
r = a%b;
z = gcd(b,r);
temp = z.s;
y.s = z.t;
y.t = temp - (q*z.t);
endif;

return y;
end
}

int main()
{
decl
node res;
int a,b,c;
enddecl

begin
c = initialize();
y = alloc();
read(a);
read(b);
res = gcd(a,b);
write(res.d);
write(res.s);
write(res.t);

return 0;
end
}
101 changes: 101 additions & 0 deletions Stage6/Test/euclidl1.expl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
type
node
{
int d;
int s;
int t;
}
List
{
int a;
int b;
node g;
List next;
}
endtype
decl
node y,z;
List head;
int insert(int a,int b,node g);
node gcd(int m,int n);
enddecl
int insert(int a,int b,node g)
{
decl
List p,q;
node temp;
enddecl
begin
p=alloc();
p.a=a;
p.b=b;
temp=alloc();
temp.d=g.d;
temp.s=g.s;
temp.t=g.t;
p.g=temp;
if (head== null) then
p.next=null;
head=p;
else
p.next=head;
head=p;
endif;
return 0;
end
}

node gcd(int m,int n){

decl
int q,r,temp;
enddecl

begin
if(n==0) then
y.d = m;
y.s = 1;
y.t = 0;
else
q = m/n;
r = m-q*n;
z = gcd(n,r);
temp = z.s;
y.s = z.t;
y.t = temp - (q*z.t);
endif;
temp=insert(m,n,y);
return y;
end
}

int main()
{
decl
int a,b,c;
node res;
enddecl

begin
c = initialize();
y = alloc();
read(a);
read(b);
head=null;
res = gcd(a,b);
write(res.d);
write(res.s);
write(res.t);
write("stack");
while(head!= null) do
write(head.a);
write(head.b);
write(head.g.d);
write(head.g.s);
write(head.g.t);
head=head.next;
write("next");
endwhile;
return 0;
end
}
Loading

0 comments on commit 0bc8a84

Please sign in to comment.