Created
November 24, 2017 16:53
-
-
Save lawrencejones/06482bf909354141bf04c70f02de674c to your computer and use it in GitHub Desktop.
Matrix multiplication AST from Wacc source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Program | |
[] | |
[ Declare | |
(ArrayType (ArrayType (Type "int"))) | |
(Ident "A") | |
(ArrayLit | |
[ ArrayLit [ Unary (Op "-") (IntLit 2) , IntLit 5 ] | |
, ArrayLit [ IntLit 3 , IntLit 7 ] | |
]) | |
, Declare | |
(ArrayType (ArrayType (Type "int"))) | |
(Ident "B") | |
(ArrayLit | |
[ ArrayLit [ IntLit 1 , IntLit 3 ] | |
, ArrayLit [ Unary (Op "-") (IntLit 4) , IntLit 7 ] | |
]) | |
, Declare | |
(ArrayType (ArrayType (Type "int"))) | |
(Ident "C") | |
(ArrayLit | |
[ ArrayLit [ IntLit 0 , IntLit 0 ] | |
, ArrayLit [ IntLit 0 , IntLit 0 ] | |
]) | |
, Declare (Type "int") (Ident "i") (IntLit 0) | |
, Declare (Type "int") (Ident "j") (IntLit 0) | |
, Declare (Type "int") (Ident "k") (IntLit 0) | |
, While | |
(Binary | |
(Op "<") (Ref (Ident "i")) (Unary (Op "len") (Ref (Ident "A")))) | |
[ Assign (VarAssign (Ident "j")) (IntLit 0) | |
, While | |
(Binary | |
(Op "<") | |
(Ref (Ident "j")) | |
(Unary (Op "len") (ArrayElem (Ident "B") [ IntLit 0 ]))) | |
[ Assign (VarAssign (Ident "k")) (IntLit 0) | |
, While | |
(Binary | |
(Op "<") (Ref (Ident "k")) (Unary (Op "len") (Ref (Ident "A")))) | |
[ Assign | |
(ArrayAssign (Ident "C") [ Ref (Ident "i") , Ref (Ident "j") ]) | |
(Binary | |
(Op "*") | |
(ArrayElem (Ident "A") [ Ref (Ident "i") , Ref (Ident "k") ]) | |
(ArrayElem (Ident "B") [ Ref (Ident "k") , Ref (Ident "j") ])) | |
, Assign | |
(VarAssign (Ident "k")) | |
(Binary (Op "+") (Ref (Ident "k")) (IntLit 1)) | |
] | |
, Assign | |
(VarAssign (Ident "j")) | |
(Binary (Op "+") (Ref (Ident "j")) (IntLit 1)) | |
] | |
, Assign | |
(VarAssign (Ident "i")) | |
(Binary (Op "+") (Ref (Ident "i")) (IntLit 1)) | |
] | |
, Assign (VarAssign (Ident "i")) (IntLit 0) | |
, Assign (VarAssign (Ident "j")) (IntLit 0) | |
, While | |
(Binary | |
(Op "<") (Ref (Ident "i")) (Unary (Op "len") (Ref (Ident "C")))) | |
[ While | |
(Binary | |
(Op "<") | |
(Ref (Ident "j")) | |
(Unary (Op "len") (ArrayElem (Ident "C") [ IntLit 0 ]))) | |
[ Builtin | |
"print" | |
(ArrayElem (Ident "C") [ Ref (Ident "i") , Ref (Ident "j") ]) | |
, Builtin "print" (StringLit ",\\t") | |
] | |
, Builtin "print" (StringLit "\\n") | |
] | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Matrix multiplication | |
begin | |
int[][] A = [[-2, 5], [ 3, 7]]; | |
int[][] B = [[ 1, 3], [-4, 7]]; | |
int[][] C = [[ 0, 0], [ 0, 0]]; | |
int i = 0; int j = 0; int k = 0; | |
while i < len A do | |
j = 0; | |
while j < len B[0] do | |
k = 0; | |
while k < len A do | |
C[i][j] = A[i][k] * B[k][j]; | |
k = k + 1 | |
done; | |
j = j + 1 | |
done; | |
i = i + 1 | |
done; | |
i = 0; j = 0; | |
while i < len C do | |
while j < len C[0] do | |
print C[i][j]; print ",\t" | |
done; | |
print "\n" | |
done | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment