Skip to content

Commit 24d825f

Browse files
author
ojd2
committed
First push
1 parent f291fd6 commit 24d825f

18 files changed

+304
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

compiler

1.37 MB
Binary file not shown.

compiler.hi

2.97 KB
Binary file not shown.

compiler.hs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
type Id = String
2+
--type Env = [(Id,Expr)]
3+
4+
5+
6+
-- Remember that "|" = Alternative options
7+
-- The expression in Haskell "deriving Show" = Print to console
8+
-- Right is name of type being defined, left is what is in that type.
9+
10+
11+
12+
-- <program> ::=
13+
-- "program" <id> ":" <pars> "." (Skel program)
14+
-- Program type is defined = takes the following expressions of "Id" or "Pars"
15+
data Program = Program Id Pars
16+
deriving Show
17+
18+
19+
20+
-- <pars> ::=
21+
-- <par> [";" <pars>] (Parallel statements)
22+
-- Pars type is defined = takes the following expression "Par".
23+
-- Alternative options are "Pars"
24+
data Pars = Par Par
25+
| Pars Pars
26+
deriving Show
27+
28+
29+
30+
-- <par> ::=
31+
-- "func" <id> <structs> (Structed expression)
32+
-- <pars> "||" <pars> (Parallel pipleline)
33+
-- "farm" <int> <pars> (Task Farm)
34+
-- Par type is defined = takes the following expressions "ParId".
35+
-- Alternative options are "ParStruct"
36+
data Par = ParId Id Structs
37+
deriving Show
38+
-- Have left Pipeline and Task Farm for now.
39+
40+
41+
42+
43+
-- <structs> ::=
44+
-- <struct> [";" <structs>] (Statements)
45+
-- Structs type is defined = takes the following expressions "Struct" and "StructAlt"
46+
data Structs = Struct Struct
47+
| StructAlt Struct Structs
48+
deriving Show
49+
50+
51+
52+
-- <struct> ::=
53+
-- <exprs> (Expression)
54+
-- | <structs> "•" <structs> (Composition)
55+
-- | "iter" <int> <structs> (Iteration)
56+
57+
-- Struct type is defined = takes the following expressions "Exprs"
58+
-- Alternative options are "StructComp" and "StructIt"
59+
data Struct = StructExp Exprs
60+
| StructComp Structs Structs
61+
| StructIt Int Structs
62+
deriving Show
63+
64+
65+
66+
-- <expr> ["," <exprs>] (Expressions)
67+
-- Exprs type is defined = takes the following expressions "Expr"
68+
-- Alternative options are "ExprAlt"
69+
data Exprs = Expression Expr
70+
| ExprAlt Expr Exprs
71+
deriving Show
72+
73+
74+
-- <expr> ::=
75+
-- <int> (integer value)
76+
-- | <string> (String value)
77+
-- | <bool> (Boolean value)
78+
-- | <id> [ "=" <exprs> ] (Identifier / assignment)
79+
-- | "raise" <id> "=" <exprs> (Raise exception)
80+
-- | <exprs> "catch" <id> <id> ":" <exprs> (Catch exception)
81+
-- | <exprs> <op> <exprs> (Binary operator)
82+
-- | "(" <exprs> ")" (Grouping)
83+
-- Expr type is Already defined = takes the following alternatives:
84+
data Expr = ExprInt Int
85+
| ExprString String
86+
| ExprBool Bool
87+
| ExprAssignment Id Exprs
88+
| ExprRaise Id Exprs
89+
| ExprCacth Exprs Id Id Exprs
90+
| ExprOp Exprs Op Exprs
91+
| ExprGroup Exprs
92+
deriving Show
93+
94+
95+
-- <op> ::=
96+
-- "+" | "*" | "-" | "div" | "<" | "<=" | "==" | "!=" (Operators)
97+
-- Op type is defined = takes the following alternatives:
98+
data Op = OpAdd
99+
| OpMultiply
100+
| OpMinus
101+
| OpDivide
102+
| OpLess
103+
| OpLessThanGreat
104+
| OpEqual
105+
| OpNotEqual
106+
deriving Show
107+
108+
109+
110+
111+
112+
113+
-- program printInt : string "int" and ExprInt of '42' int
114+
-- test = Program "printInt" ( Par ( ParId "Int" ( Struct ( StructExp ( Expression ( ExprInt 42 ) ) ) ) ) )
115+
116+
-- program printString : func String "Hello World!".
117+
-- test2 = Program "printString" ( Par ( ParId "String" ( Struct ( StructExp ( Expression ( ExprString "Hello World!" ) ) ) ) ) )
118+
119+
-- program assignBoolTrue : func Bool True.
120+
-- test3 = Program "assignBoolTrue" ( Par ( ParId "Bool" ( Struct ( StructExp ( Expression ( ExprBool True ) ) ) ) ) )
121+
122+
-- program assignBoolTrue : func Bool True.
123+
-- test4 = Program "assignBoolFalse" ( Par ( ParId "Bool" ( Struct ( StructExp ( Expression ( ExprBool False ) ) ) ) ) )
124+
125+
-- program opAddInts : func OpAdd ExprInt 10, OpAdd ExprInt 10
126+
--test5 = Program "opAddInts" ( Par ( ParId "OpAdd" ( Struct ( StructExp ( Expression ( ExprOp (Expression (ExprInt 10) ) (OpAdd) (Expression (ExprInt 5) ) ) ) ) ) ) )
127+
128+
-- program opMinusInts : func OpMinus ExprInt 100, OpAdd ExprInt 35
129+
test6 = Program "opMinusInts" ( Par ( ParId "OpMinus" ( Struct ( StructExp ( Expression ( ExprOp (Expression (ExprInt 100) ) (OpMinus) (Expression (ExprInt 35) ) ) ) ) ) ) )
130+
131+
132+
133+
134+
135+
136+
137+
138+
139+
-- Print out test program above using AST declarations
140+
main = print test6
141+

compiler.o

70.1 KB
Binary file not shown.

jasmin.jar

126 KB
Binary file not shown.

jrun

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java -cp /tmp $*

readme.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// INSTRUCTIONS
2+
3+
To compile compiler.hs
4+
5+
6+
-- To run, go to terminal, cd into compiler.hs folder and then type ghc compiler.hs
7+
-- ./compiler to list test program in console.
8+
9+
10+
To run jasmin on j file:
11+
12+
-- First compile then run java -jar jasmin.jar test.j
13+
14+
-- Finally, java test
15+

test.class

335 Bytes
Binary file not shown.

test.j

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.class public test
2+
.super java/lang/Object
3+
4+
; standard initializer
5+
; default constructor
6+
7+
.method public <init>()V
8+
aload_0 ; push this
9+
invokespecial java/lang/Object/<init>()V ; call super
10+
return
11+
.end method
12+
13+
.method public static main([Ljava/lang/String;)V
14+
15+
; allocate stack big enough to hold 2 items
16+
.limit stack 2
17+
.limit locals 2
18+
19+
; push java.lang.System.out (type PrintStream)
20+
getstatic java/lang/System/out Ljava/io/PrintStream;
21+
; push int to be printed
22+
ldc 42
23+
; invoke println
24+
invokevirtual java/io/PrintStream/println(I)V ; int print
25+
; terminate main
26+
return
27+
28+
.end method

test2.class

367 Bytes
Binary file not shown.

test2.j

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.class public test2
2+
.super java/lang/Object
3+
4+
; standard initializer
5+
; default constructor
6+
7+
.method public <init>()V
8+
aload_0
9+
invokespecial java/lang/Object/<init>()V
10+
return
11+
.end method
12+
13+
.method public static main([Ljava/lang/String;)V
14+
15+
; allocate stack big enough to hold 1 item
16+
.limit stack 2
17+
.limit locals 1
18+
19+
; push java.lang.System.out (type PrintStream)
20+
getstatic java/lang/System/out Ljava/io/PrintStream;
21+
; push string to be printed
22+
ldc "Hello World!"
23+
; invoke println
24+
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V ; string print
25+
; terminate main
26+
return
27+
28+
.end method

test3.class

337 Bytes
Binary file not shown.

test3.j

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.class public test3
2+
.super java/lang/Object
3+
4+
; standard initializer
5+
; default constructor
6+
7+
.method public <init>()V
8+
aload_0 ; push this
9+
invokespecial java/lang/Object/<init>()V ; call super
10+
return
11+
.end method
12+
13+
.method public static main([Ljava/lang/String;)V
14+
15+
; allocate stack big enough to hold 1 item
16+
.limit stack 2
17+
.limit locals 1
18+
19+
; push java.lang.System.out (type PrintStream)
20+
getstatic java/lang/System/out Ljava/io/PrintStream;
21+
; push int to be printed
22+
; 0 = f, 1 = t
23+
ldc 1
24+
; invoke println
25+
invokevirtual java/io/PrintStream/println(Z)V ; bool print (Z)
26+
; terminate main
27+
return
28+
29+
.end method

test4.class

337 Bytes
Binary file not shown.

test4.j

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.class public test4
2+
.super java/lang/Object
3+
4+
; standard initializer
5+
; default constructor
6+
7+
.method public <init>()V
8+
aload_0 ; push this
9+
invokespecial java/lang/Object/<init>()V ; call super
10+
return
11+
.end method
12+
13+
.method public static main([Ljava/lang/String;)V
14+
15+
; allocate stack big enough to hold 1 item
16+
.limit stack 2
17+
.limit locals 1
18+
19+
; push java.lang.System.out (type PrintStream)
20+
getstatic java/lang/System/out Ljava/io/PrintStream;
21+
; push int to be printed
22+
; 0 = f, 1 = t
23+
ldc 0
24+
; invoke println
25+
invokevirtual java/io/PrintStream/println(Z)V ; bool print (Z)
26+
; terminate main
27+
return
28+
29+
.end method

test5.class

348 Bytes
Binary file not shown.

test5.j

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.class public test5
2+
.super java/lang/Object
3+
4+
; standard initializer
5+
; default constructor
6+
7+
.method public <init>()V
8+
aload_0 ; push this
9+
invokespecial java/lang/Object/<init>()V ; call super
10+
return
11+
.end method
12+
13+
.method public static main([Ljava/lang/String;)V
14+
15+
; allocate stack big enough to hold 3 items
16+
.limit stack 3
17+
.limit locals 2 ; should be fine with 2
18+
19+
; push java.lang.System.out (type PrintStream)
20+
getstatic java/lang/System/out Ljava/io/PrintStream;
21+
; push ints to be printed
22+
23+
ldc 10 ; load int
24+
ldc 5 ; load int
25+
iadd ; add int
26+
istore_0 ; store result in variable
27+
iload 0 ; load variable, push to top of stack
28+
; invoke println
29+
invokevirtual java/io/PrintStream/println(I)V ; int print result
30+
; terminate main
31+
return
32+
33+
.end method

0 commit comments

Comments
 (0)