Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat while statement #24

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions codegen/jasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type JASM struct {
endIfLabel string
elseLabel string
repeatLabel string
whileTestLabel string
nextStatementLabel string
st *SymbolTable
}

Expand Down Expand Up @@ -111,6 +113,21 @@ func (j *JASM) FinishRepeatStatement() {
j.addOpcode("ifeq", j.repeatLabel)
}

func (j *JASM) StartWhileStatement() {
j.whileTestLabel = j.newLabel()
j.nextStatementLabel = j.newLabel()
j.addLabel(j.whileTestLabel)
}

func (j *JASM) FinishWhileStatement() {
j.addGotoOpcode(j.whileTestLabel)
j.addLabel(j.nextStatementLabel)
}

func (j *JASM) StartWhileBlock() {
j.addOpcode("ifeq", j.nextStatementLabel)
}

func (j *JASM) AddOperatorOpcode(op string) {
pt1 := j.pst.Pop()
pt2 := j.pst.Pop()
Expand Down
12 changes: 12 additions & 0 deletions codegen/tree_shape_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ func (t *TreeShapeListener) ExitRepeatStatement(ctx *parsing.RepeatStatementCont
t.jasm.FinishRepeatStatement()
}

func (t *TreeShapeListener) EnterWhileStatement(ctx *parsing.WhileStatementContext) {
t.jasm.StartWhileStatement()
}

func (t *TreeShapeListener) ExitWhileStatement(ctx *parsing.WhileStatementContext) {
t.jasm.FinishWhileStatement()
}

func (t *TreeShapeListener) EnterWhileBlock(ctx *parsing.WhileBlockContext) {
t.jasm.StartWhileBlock()
}

func (t *TreeShapeListener) Code() string {
return t.jasm.Code()
}
6 changes: 5 additions & 1 deletion parser/Pascal.g4
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,11 @@ repetetiveStatement
;

whileStatement
: WHILE expression DO statement
: WHILE expression whileBlock
;

whileBlock
: DO statement
;

repeatStatement
Expand Down
58 changes: 58 additions & 0 deletions tests/expected_jasm_files/while.jasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Code generated by POJ 0.1
public class while {
public static main([java/lang/String)V {
sipush 10
istore 1
L1:
iload 1
sipush 20
if_icmpge L3
iconst 1
goto L4
L3:
iconst 0
L4:
ifeq L2
getstatic java/lang/System.out java/io/PrintStream
ldc "i="
invokevirtual java/io/PrintStream.print(java/lang/String)V
getstatic java/lang/System.out java/io/PrintStream
iload 1
invokevirtual java/io/PrintStream.print(I)V
getstatic java/lang/System.out java/io/PrintStream
invokevirtual java/io/PrintStream.println()V
iload 1
sipush 1
iadd
istore 1
goto L1
L2:
sipush 20
istore 1
L5:
iload 1
sipush 10
if_icmpgt L7
iconst 1
goto L8
L7:
iconst 0
L8:
ifeq L6
getstatic java/lang/System.out java/io/PrintStream
ldc "dont write i="
invokevirtual java/io/PrintStream.print(java/lang/String)V
getstatic java/lang/System.out java/io/PrintStream
iload 1
invokevirtual java/io/PrintStream.print(I)V
getstatic java/lang/System.out java/io/PrintStream
invokevirtual java/io/PrintStream.println()V
iload 1
sipush 1
iadd
istore 1
goto L5
L6:
return
}
}
19 changes: 19 additions & 0 deletions tests/pascal_programs/while.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
program WhileExample;
var
i: integer;
begin
i := 10;
while i < 20 do
begin
writeln ('i=', i);
i := i + 1;
end;

i := 20;
while i <= 10 do
begin
writeln ('dont write i=', i);
i := i + 1;
end;

end.