commit 5f28f80c4e25a56cd444914c2f0b3da5e7fdb088 Author: Lizzy Hunt Date: Mon Apr 10 09:17:11 2023 -0600 Initial commit - building diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..409dd2b Binary files /dev/null and b/.DS_Store differ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8fbb478 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Main", + "request": "launch", + "mainClass": "main.Main", + "projectName": "p5-compiler_25e45799" + }, + { + "type": "java", + "name": "Main", + "request": "launch", + "mainClass": "Main", + "projectName": "p5-compiler_25e45799" + }, + { + "type": "java", + "name": "Current File", + "request": "launch", + "mainClass": "${file}" + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0687886 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "java.project.referencedLibraries": [ + "lib/**/*.jar", + "antlr-4.9.1-complete.jar" + ], + "rpc.enabled": false, + "java.debug.settings.onBuildFailureProceed": true +} diff --git a/antlr-4.9.1-complete.jar b/antlr-4.9.1-complete.jar new file mode 100644 index 0000000..1bb7e8c Binary files /dev/null and b/antlr-4.9.1-complete.jar differ diff --git a/data/.DS_Store b/data/.DS_Store new file mode 100644 index 0000000..6ad1891 Binary files /dev/null and b/data/.DS_Store differ diff --git a/data/examples/.DS_Store b/data/examples/.DS_Store new file mode 100644 index 0000000..bd8fb93 Binary files /dev/null and b/data/examples/.DS_Store differ diff --git a/data/examples/Mars4_5.jar b/data/examples/Mars4_5.jar new file mode 100644 index 0000000..0021281 Binary files /dev/null and b/data/examples/Mars4_5.jar differ diff --git a/data/examples/test1.asm b/data/examples/test1.asm new file mode 100644 index 0000000..b849530 --- /dev/null +++ b/data/examples/test1.asm @@ -0,0 +1,35 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "Hello world" diff --git a/data/examples/test10.asm b/data/examples/test10.asm new file mode 100644 index 0000000..4d0bb1f --- /dev/null +++ b/data/examples/test10.asm @@ -0,0 +1,434 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for fib +fib: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 0 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel0 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel1 +datalabel0: +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 1 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel2 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel3 +datalabel2: +datalabel3: +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +sub $t1 $t1 $t2 +sw $t1 -16($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -20($sp) +# Calling function fib +# Save $ra to a register +move $t1 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +sw $t1 -16($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t3 -4 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of i. +lw $t2 0($t3) +li $t3 2 +sub $t2 $t2 $t3 +sw $t2 -20($sp) +# Update the stack pointer +add $sp $sp -16 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 16 +# Restore $t0-9 registers +lw $t0 -12($sp) +lw $t1 -16($sp) +# Restore $ra +move $ra $t1 +# Get return value off stack +lw $t1 -24($sp) +add $t0 $t0 $t1 +sw $t0 -8($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel4 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 0 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 1 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 2 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 3 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 4 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 5 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 6 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 7 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 8 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 9 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 10 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel4: .asciiz "This program prints the first 11 numbers of the Fibonacci sequence" diff --git a/data/examples/test11.asm b/data/examples/test11.asm new file mode 100644 index 0000000..f72fb29 --- /dev/null +++ b/data/examples/test11.asm @@ -0,0 +1,92 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 10 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel2 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 0 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 4 +j datalabel1 +datalabel2: +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 0 through 9." diff --git a/data/examples/test12.asm b/data/examples/test12.asm new file mode 100644 index 0000000..dbb716b --- /dev/null +++ b/data/examples/test12.asm @@ -0,0 +1,213 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for fib +fib: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 0 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel0 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel1 +datalabel0: +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 1 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel2 +li $t0 1 +sw $t0 -8($sp) +jr $ra +j datalabel3 +datalabel2: +datalabel3: +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +sub $t1 $t1 $t2 +sw $t1 -16($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -20($sp) +# Calling function fib +# Save $ra to a register +move $t1 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +sw $t1 -16($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t3 -4 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of i. +lw $t2 0($t3) +li $t3 2 +sub $t2 $t2 $t3 +sw $t2 -20($sp) +# Update the stack pointer +add $sp $sp -16 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 16 +# Restore $t0-9 registers +lw $t0 -12($sp) +lw $t1 -16($sp) +# Restore $ra +move $ra $t1 +# Get return value off stack +lw $t1 -24($sp) +add $t0 $t0 $t1 +sw $t0 -8($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# println +la $a0 datalabel4 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +datalabel5: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 12 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel6 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +# Calling function fib +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fib +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 4 +j datalabel5 +datalabel6: +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel4: .asciiz "This program prints the first 12 numbers of the Fibonacci sequence." diff --git a/data/examples/test13.asm b/data/examples/test13.asm new file mode 100644 index 0000000..a459701 --- /dev/null +++ b/data/examples/test13.asm @@ -0,0 +1,296 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -40 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +li $t1 0 +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 0 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -40 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +li $t1 2 +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 2 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -44 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -40 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -44 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 -44 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t3 -44 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of i. +lw $t2 0($t3) +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -44 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +datalabel1: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -44 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 10 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel2 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -44 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# complete assignment statement with store +sw $t1 0($t0) +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 44 +j datalabel1 +datalabel2: +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 3 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 6 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -40 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Evaluate the index expression and store in a register. +li $t2 6 +# Multiply the index by -4. +li $t3 4 +mul $t2 $t2 $t3 +# Add the index offset to the offset. +add $t1 $t1 $t2 +# Load the value of a. +lw $t0 0($t1) +li $t1 6 +mult $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This should print 0, 2, 2, 3, 6 and 36" diff --git a/data/examples/test14.asm b/data/examples/test14.asm new file mode 100644 index 0000000..4c16021 --- /dev/null +++ b/data/examples/test14.asm @@ -0,0 +1,289 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for sum +sum: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# i +# sum +# n +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -16 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t0 -20 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +datalabel0: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -16 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +# Get n's offset from $sp from the symbol table and initialize n's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of n. +lw $t1 0($t2) +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel1 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -20 +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of sum. +lw $t1 0($t2) +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t3 16 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Get x's base address from the stack. +lw $t3 0($t3) +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t5 4 +# Add the stack pointer address to the offset. +add $t5 $t5 $sp +# Load the value of i. +lw $t4 0($t5) +# Multiply the index by -4. +li $t5 4 +mul $t4 $t4 $t5 +# Add the index offset to the offset. +add $t3 $t3 $t4 +# Load the value of x. +lw $t2 0($t3) +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 20 +j datalabel0 +datalabel1: +# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later. +li $t1 -20 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of sum. +lw $t0 0($t1) +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# i +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel2 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 -44 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 0 +# complete assignment statement with store +sw $t1 0($t0) +datalabel3: +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t1 -44 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of i. +lw $t0 0($t1) +li $t1 10 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel4 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -44 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Evaluate the index expression and store in a register. +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# Multiply the index by -4. +li $t2 4 +mul $t1 $t1 $t2 +# Add the index offset to the offset. +add $t0 $t0 $t1 +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +# complete assignment statement with store +sw $t1 0($t0) +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of i. +lw $t1 0($t2) +li $t2 1 +add $t1 $t1 $t2 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 44 +j datalabel3 +datalabel4: +# println +# Calling function sum +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -48($sp) +# Evaluate parameters and save to stack +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -40 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Put the address of the array into the register. +# Use the address of a. +move $t1 $t2 +sw $t1 -52($sp) +li $t1 4 +sw $t1 -56($sp) +# Update the stack pointer +add $sp $sp -48 +# Call the function +jal sum +# Restore the stack pointer +add $sp $sp 48 +# Restore $t0-9 registers +lw $t0 -48($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -60($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function sum +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -48($sp) +# Evaluate parameters and save to stack +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -40 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Put the address of the array into the register. +# Use the address of a. +move $t1 $t2 +sw $t1 -52($sp) +li $t1 8 +sw $t1 -56($sp) +# Update the stack pointer +add $sp $sp -48 +# Call the function +jal sum +# Restore the stack pointer +add $sp $sp 48 +# Restore $t0-9 registers +lw $t0 -48($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -60($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel2: .asciiz "This should print 6 and 28" diff --git a/data/examples/test2.asm b/data/examples/test2.asm new file mode 100644 index 0000000..7f3312a --- /dev/null +++ b/data/examples/test2.asm @@ -0,0 +1,89 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 7 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 3 +li $t1 4 +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 14 +li $t1 2 +div $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 7 +li $t1 1 +mult $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +li $t0 7 +li $t1 2 +mult $t0 $t1 +mflo $t0 +li $t1 2 +div $t0 $t1 +mflo $t0 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7 7 7 7 (separated by newlines)" diff --git a/data/examples/test3.asm b/data/examples/test3.asm new file mode 100644 index 0000000..69f73f3 --- /dev/null +++ b/data/examples/test3.asm @@ -0,0 +1,73 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 3 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 4 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints the number 7" diff --git a/data/examples/test4.asm b/data/examples/test4.asm new file mode 100644 index 0000000..db276ba --- /dev/null +++ b/data/examples/test4.asm @@ -0,0 +1,162 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 3 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# Update the stack pointer. +addi $sp $sp -8 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 5 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 0 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Entering a new scope. +# Symbols in symbol table: +# println +# b +# Update the stack pointer. +addi $sp $sp -4 +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 9 +# complete assignment statement with store +sw $t1 0($t0) +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +sub $t1 $zero $t1 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 0 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 0 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 4 +# complete assignment statement with store +sw $t1 0($t0) +# Exiting scope. +addi $sp $sp 8 +# println +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7 7" diff --git a/data/examples/test5.asm b/data/examples/test5.asm new file mode 100644 index 0000000..76837bc --- /dev/null +++ b/data/examples/test5.asm @@ -0,0 +1,155 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for foo +foo: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +li $t0 7 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for fum +fum: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 9 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 12 +# complete assignment statement with store +sw $t1 0($t0) +# println +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t1 -8 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of b. +lw $t0 0($t1) +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of a. +lw $t1 0($t2) +sub $t0 $t0 $t1 +li $t1 4 +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Calling function foo +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal foo +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Calling function foo +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal foo +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Calling function fum +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal fum +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7 7" diff --git a/data/examples/test6.asm b/data/examples/test6.asm new file mode 100644 index 0000000..1668afa --- /dev/null +++ b/data/examples/test6.asm @@ -0,0 +1,138 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for add +add: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# i +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of y. +lw $t1 0($t2) +add $t0 $t0 $t1 +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# b +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +li $t1 3 +sw $t1 -16($sp) +li $t1 4 +sw $t1 -20($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 5 +# complete assignment statement with store +sw $t1 0($t0) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t0 -8 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 2 +# complete assignment statement with store +sw $t1 0($t0) +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -12($sp) +# Evaluate parameters and save to stack +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t2 -4 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of a. +lw $t1 0($t2) +sw $t1 -16($sp) +# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of b. +lw $t1 0($t2) +sw $t1 -20($sp) +# Update the stack pointer +add $sp $sp -12 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 12 +# Restore $t0-9 registers +lw $t0 -12($sp) +# Restore $ra +move $ra $t0 +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7" diff --git a/data/examples/test7.asm b/data/examples/test7.asm new file mode 100644 index 0000000..8414044 --- /dev/null +++ b/data/examples/test7.asm @@ -0,0 +1,141 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for identity +identity: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +sw $t0 -8($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for add +add: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of y. +lw $t1 0($t2) +add $t0 $t0 $t1 +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function identity +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 7 +sw $t1 -8($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal identity +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -12($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 3 +sw $t1 -8($sp) +li $t1 4 +sw $t1 -12($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -16($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7 7" diff --git a/data/examples/test8.asm b/data/examples/test8.asm new file mode 100644 index 0000000..b9a3263 --- /dev/null +++ b/data/examples/test8.asm @@ -0,0 +1,164 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for add +add: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of x. +lw $t0 0($t1) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t2 -8 +# Add the stack pointer address to the offset. +add $t2 $t2 $sp +# Load the value of y. +lw $t1 0($t2) +add $t0 $t0 $t1 +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for add2 +add2: +# Entering a new scope. +# Symbols in symbol table: +# println +# x +# y +# return +# Update the stack pointer. +addi $sp $sp -0 +# Calling function add +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -16($sp) +# Evaluate parameters and save to stack +# Calling function add +# Save $ra to a register +move $t1 $ra +# Save $t0-9 registers +sw $t0 -16($sp) +sw $t1 -20($sp) +# Evaluate parameters and save to stack +# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later. +li $t3 -4 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of x. +lw $t2 0($t3) +sw $t2 -24($sp) +# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later. +li $t3 -8 +# Add the stack pointer address to the offset. +add $t3 $t3 $sp +# Load the value of y. +lw $t2 0($t3) +sw $t2 -28($sp) +# Update the stack pointer +add $sp $sp -20 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 20 +# Restore $t0-9 registers +lw $t0 -16($sp) +lw $t1 -20($sp) +# Restore $ra +move $ra $t1 +# Get return value off stack +lw $t1 -32($sp) +sw $t1 -20($sp) +li $t1 1 +sw $t1 -24($sp) +# Update the stack pointer +add $sp $sp -16 +# Call the function +jal add +# Restore the stack pointer +add $sp $sp 16 +# Restore $t0-9 registers +lw $t0 -16($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -28($sp) +sw $t0 -12($sp) +jr $ra +# Exiting scope. +addi $sp $sp 0 +jr $ra + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# println +# Calling function add2 +# Save $ra to a register +move $t0 $ra +# Save $t0-9 registers +sw $t0 -4($sp) +# Evaluate parameters and save to stack +li $t1 2 +sw $t1 -8($sp) +li $t1 4 +sw $t1 -12($sp) +# Update the stack pointer +add $sp $sp -4 +# Call the function +jal add2 +# Restore the stack pointer +add $sp $sp 4 +# Restore $t0-9 registers +lw $t0 -4($sp) +# Restore $ra +move $ra $t0 +# Get return value off stack +lw $t0 -16($sp) +move $a0 $t0 +li $v0 1 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints 7" diff --git a/data/examples/test9.asm b/data/examples/test9.asm new file mode 100644 index 0000000..db843e0 --- /dev/null +++ b/data/examples/test9.asm @@ -0,0 +1,239 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# code for main +main: +# Entering a new scope. +# Symbols in symbol table: +# a +# println +# return +# Update the stack pointer. +addi $sp $sp -0 +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t0 -4 +# Add the stack pointer address to the offset. +add $t0 $t0 $sp +# Compute rhs for assignment = +li $t1 3 +# complete assignment statement with store +sw $t1 0($t0) +# println +la $a0 datalabel0 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t0 $t1 +subi $t0 $t0 1 +bne $t0 $zero datalabel1 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel3 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel2 +datalabel1: +datalabel2: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t1 $t0 +subi $t0 $t0 1 +bne $t0 $zero datalabel4 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel6 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel5 +datalabel4: +datalabel5: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t1 $t0 +subi $t0 $t0 1 +bne $t0 $zero datalabel7 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel9 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel8 +datalabel7: +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel10 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +datalabel8: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 3 +slt $t0 $t1 $t0 +bne $t0 $zero datalabel11 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel13 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel12 +datalabel11: +datalabel12: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 3 +sub $t0 $t0 $t1 +bne $t0 $zero datalabel14 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel16 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel15 +datalabel14: +datalabel15: +# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later. +li $t1 -4 +# Add the stack pointer address to the offset. +add $t1 $t1 $sp +# Load the value of a. +lw $t0 0($t1) +li $t1 4 +slt $t0 $t0 $t1 +bne $t0 $zero datalabel17 +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel19 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +j datalabel18 +datalabel17: +# Entering a new scope. +# Symbols in symbol table: +# println +# Update the stack pointer. +addi $sp $sp -4 +# println +la $a0 datalabel20 +li $v0 4 +syscall +la $a0 newline +li $v0 4 +syscall +# Exiting scope. +addi $sp $sp 4 +datalabel18: +# Exiting scope. +addi $sp $sp 0 +li $v0 10 +syscall + +# All memory structures are placed after the +# .data assembler directive +.data + +newline: .asciiz "\n" +datalabel0: .asciiz "This program prints [1..5] correct." +datalabel3: .asciiz "1 correct" +datalabel6: .asciiz "2 not correct" +datalabel9: .asciiz "2 not correct" +datalabel10: .asciiz "2 correct" +datalabel13: .asciiz "3 correct" +datalabel16: .asciiz "4 correct" +datalabel19: .asciiz "5 not correct" +datalabel20: .asciiz "5 correct" diff --git a/data/test b/data/test new file mode 100755 index 0000000..89d5b0c --- /dev/null +++ b/data/test @@ -0,0 +1,23 @@ +#! /bin/bash + +java -jar ./Mars4_5.jar test1.asm +java -jar ./Mars4_5.jar test2.asm +java -jar ./Mars4_5.jar test3.asm +java -jar ./Mars4_5.jar test4.asm +java -jar ./Mars4_5.jar test5.asm +java -jar ./Mars4_5.jar test6.asm +java -jar ./Mars4_5.jar test7.asm +java -jar ./Mars4_5.jar test8.asm +java -jar ./Mars4_5.jar test9.asm +java -jar ./Mars4_5.jar test10.asm +java -jar ./Mars4_5.jar test11.asm +java -jar ./Mars4_5.jar test12.asm +java -jar ./Mars4_5.jar test13.asm +java -jar ./Mars4_5.jar test14.asm + +# This loop runs the Java code to create the .asm then runs the .asm. +#for i in {1..14} +#do +# java -cp ../../out/production/p5-compiler:../../antlr-4.9.1-complete.jar main.Main ../test$i.c +# java -jar ./Mars4_5.jar ../test$i.asm +#done diff --git a/data/test1.asm b/data/test1.asm new file mode 100644 index 0000000..6c203dc --- /dev/null +++ b/data/test1.asm @@ -0,0 +1,13 @@ +# All program code is placed after the +# .text assembler directive +.text + +# Declare main as a global function +.globl main + +j main + +# All memory structures are placed after the +# .data assembler directive +.data + diff --git a/data/test1.c b/data/test1.c new file mode 100644 index 0000000..e336f81 --- /dev/null +++ b/data/test1.c @@ -0,0 +1,3 @@ +void main() { + println("Hello world"); +} diff --git a/data/test10.c b/data/test10.c new file mode 100644 index 0000000..a90a21f --- /dev/null +++ b/data/test10.c @@ -0,0 +1,20 @@ +int fib(int i) { + if (i == 0) return 1; + if (i == 1) return 1; + return fib(i-1) + fib(i-2); +} + +void main() { + println("This program prints the first 11 numbers of the Fibonacci sequence"); + println(fib(0)); // 1 + println(fib(1)); // 1 + println(fib(2)); // 2 + println(fib(3)); // 3 + println(fib(4)); // 5 + println(fib(5)); // 8 + println(fib(6)); // 13 + println(fib(7)); // 21 + println(fib(8)); // 34 + println(fib(9)); // 55 + println(fib(10)); // 89 +} diff --git a/data/test11.c b/data/test11.c new file mode 100644 index 0000000..865bcf8 --- /dev/null +++ b/data/test11.c @@ -0,0 +1,9 @@ +void main() { + int i; + i = 0; + println("This program prints 0 through 9."); + while (i < 10) { + println(i); + i = i + 1; + } +} diff --git a/data/test12.c b/data/test12.c new file mode 100644 index 0000000..4bb7f58 --- /dev/null +++ b/data/test12.c @@ -0,0 +1,15 @@ +int fib(int i) { + if (i == 0) return 1; + if (i == 1) return 1; + return fib(i-1) + fib(i-2); +} + +void main() { + int i; + i = 0; + println("This program prints the first 12 numbers of the Fibonacci sequence."); + while (i < 12) { + println(fib(i)); + i = i + 1; + } +} diff --git a/data/test13.c b/data/test13.c new file mode 100644 index 0000000..c1a5c72 --- /dev/null +++ b/data/test13.c @@ -0,0 +1,23 @@ +void main() { + int a[10]; + int i; + + println("This should print 0, 2, 2, 3, 6 and 36"); + a[0] = 0; + println(a[0]); + a[2] = 2; + println(a[2]); + + i = 2; + a[i] = i; + println(a[i]); + + i = 0; + while (i < 10) { + a[i] = i; + i = i + 1; + } + println(a[3]); + println(a[6]); + println(a[6]*6); +} diff --git a/data/test14.c b/data/test14.c new file mode 100644 index 0000000..45565fa --- /dev/null +++ b/data/test14.c @@ -0,0 +1,27 @@ +// Sum the first n elements of the array +int sum(int x[], int n) { + int i; + int sum; + i = 0; + sum = 0; + while (i < n) { + sum = sum + x[i]; + i = i + 1; + } + return sum; +} + +void main() { + int a[10]; + int i; + + println("This should print 6 and 28"); + i = 0; + while (i < 10) { + a[i] = i; + i = i + 1; + } + + println(sum(a, 4)); + println(sum(a, 8)); +} diff --git a/data/test2.c b/data/test2.c new file mode 100644 index 0000000..136ba74 --- /dev/null +++ b/data/test2.c @@ -0,0 +1,8 @@ +void main() { + println("This program prints 7 7 7 7 7 (separated by newlines)"); + println(7); + println(3+4); + println(14/2); + println(7*1); + println((7*2)/2); +} diff --git a/data/test3.c b/data/test3.c new file mode 100644 index 0000000..1083434 --- /dev/null +++ b/data/test3.c @@ -0,0 +1,9 @@ +void main() { + int a; + int b; + + println("This program prints the number 7"); + a = 3; + b = 4; + println(a+b); +} diff --git a/data/test4.c b/data/test4.c new file mode 100644 index 0000000..c4c31c2 --- /dev/null +++ b/data/test4.c @@ -0,0 +1,21 @@ +void main() { + int a; + int b; + + println("This program prints 7 7 7"); + a = 3; + b = 2; + { + int a; + a = 5; + println(a+b); + { + int b; + b = 9; + a = -2; + println(a+b); + } + b = 4; + } + println(a+b); +} diff --git a/data/test5.c b/data/test5.c new file mode 100644 index 0000000..835db83 --- /dev/null +++ b/data/test5.c @@ -0,0 +1,19 @@ +void foo() { + println(7); +} + +void fum() { + int a; + int b; + a = 9; + b = 12; + println(b-a+4); + + foo(); +} + +void main() { + println("This program prints 7 7 7"); + foo(); + fum(); +} diff --git a/data/test6.c b/data/test6.c new file mode 100644 index 0000000..03de49c --- /dev/null +++ b/data/test6.c @@ -0,0 +1,15 @@ +void add(int x, int y) { + int i; + println(x+y); +} + +void main() { + int a; + int b; + println("This program prints 7 7"); + add(3, 4); + + a = 5; + b = 2; + add(a, b); +} diff --git a/data/test7.c b/data/test7.c new file mode 100644 index 0000000..e7f45a5 --- /dev/null +++ b/data/test7.c @@ -0,0 +1,14 @@ +int identity(int x) { + return x; +} + +int add(int x, int y) { + return x+y; +} + +void main() { + println("This program prints 7 7"); + println(identity(7)); + println(add(3, 4)); +} + diff --git a/data/test8.c b/data/test8.c new file mode 100644 index 0000000..df0f2ec --- /dev/null +++ b/data/test8.c @@ -0,0 +1,14 @@ + +int add(int x, int y) { + return x+y; +} + +int add2(int x, int y) { + return add(add(x,y),1); +} + +void main() { + println("This program prints 7"); + println(add2(2, 4)); +} + diff --git a/data/test9.c b/data/test9.c new file mode 100644 index 0000000..1aacb59 --- /dev/null +++ b/data/test9.c @@ -0,0 +1,27 @@ +void main() { + int a; + a = 3; + println("This program prints [1..5] correct."); + if (a < 4) { + println("1 correct"); + } + if (a > 4) { + println("2 not correct"); + } + if (a > 4) { + println("2 not correct"); + } else { + println("2 correct"); + } + if (a <= 3) { + println("3 correct"); + } + if (a == 3) { + println("4 correct"); + } + if (a >= 4) { + println("5 not correct"); + } else { + println("5 correct"); + } +} diff --git a/main/Main.java b/main/Main.java new file mode 100644 index 0000000..9df6a18 --- /dev/null +++ b/main/Main.java @@ -0,0 +1,109 @@ +package main; + +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import parser.CminusLexer; +import parser.CminusParser; +import submit.ASTVisitor; +import org.antlr.v4.runtime.Parser; +import submit.RegisterAllocator; +import submit.SymbolTable; +import submit.ast.Node; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Properties; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class Main { + private static Logger LOGGER; + + /** + * @param args the command line arguments + * @throws java.io.IOException + */ + public static void main(String[] args) throws IOException { + // Logging setup + Level level = Level.INFO; + + // TODO Enable trace-level code as needed. When true, LOGGER.fine() statements + // will be visible. + final boolean trace = true; + if (trace) { + level = Level.ALL; + } + + Properties props = System.getProperties(); + props.setProperty("java.util.logging.SimpleFormatter.format", "%5$s%6$s%n"); + Logger.getLogger("").setLevel(level); + for (Handler handler : Logger.getLogger("").getHandlers()) { + handler.setLevel(level); + } + LOGGER = Logger.getLogger(Parser.class.getName()); + + if (args.length < 1) { + throw new RuntimeException("Be sure to add your test C- file as a command-line parameter."); + } + final String filename = args[0]; + + LOGGER.info(""); + LOGGER.info("Parsing " + filename + "\n"); + LOGGER.info(""); + final CharStream charStream = CharStreams.fromFileName(filename); + CminusLexer lexer = new CminusLexer(charStream); + CommonTokenStream tokens = new CommonTokenStream(lexer); + CminusParser parser = new CminusParser(tokens); + parser.setBuildParseTree(true); + CminusParser.ProgramContext programCtx = parser.program(); + + LOGGER.info(""); + LOGGER.info("Building abstract syntax tree"); + LOGGER.info(""); + ASTVisitor v = new ASTVisitor(LOGGER); + Node ast = v.visitProgram(programCtx); + SymbolTable symbolTable = v.getSymbolTable(); + + LOGGER.info(""); + LOGGER.info("MIPS code:"); + LOGGER.info(""); + StringBuilder code = new StringBuilder(); + StringBuilder data = new StringBuilder(); + RegisterAllocator regAllocator = new RegisterAllocator(); + try { + ast.toMIPS(code, data, symbolTable, regAllocator); + } finally { + StringBuilder mips = new StringBuilder(); + mips.append("# All program code is placed after the\n" + + "# .text assembler directive\n" + + ".text\n" + + "\n" + + "# Declare main as a global function\n" + + ".globl\tmain\n\nj main\n"); + mips.append(code.toString()); + mips.append("\n# All memory structures are placed after the\n" + + "# .data assembler directive\n" + + ".data\n" + + "\n"); + mips.append(data.toString()); + + // Write MIPS code to stdout + LOGGER.info(mips.toString()); + + // Write MIPS code to output file for easy loading into MARS + final String fn = filename.substring(0, filename.length() - 1) + "asm"; + try { + FileWriter myWriter = new FileWriter(fn); + myWriter.write(mips.toString()); + myWriter.close(); + } catch (IOException e) { + LOGGER.severe("Error writing to file " + fn + "\n"); + e.printStackTrace(); + } + } + } + +} diff --git a/parser/Cminus.interp b/parser/Cminus.interp new file mode 100644 index 0000000..af140be --- /dev/null +++ b/parser/Cminus.interp @@ -0,0 +1,140 @@ +token literal names: +null +',' +';' +'[' +']' +'void' +'(' +')' +'int' +'bool' +'char' +'[]' +'{' +'}' +'if' +'else' +'while' +'return' +'break' +'=' +'+=' +'-=' +'*=' +'/=' +'++' +'--' +'||' +'&&' +'<=' +'<' +'>' +'>=' +'==' +'!=' +'+' +'-' +'*' +'/' +'%' +'?' +'true' +'false' +null +null +null +null +'!' +null +null + +token symbolic names: +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +ID +NUMCONST +STRINGCONST +CHARCONST +BANG +WS +COMMENT + +rule names: +program +declaration +varDeclaration +varDeclId +funDeclaration +typeSpecifier +param +paramId +statement +compoundStmt +expressionStmt +ifStmt +whileStmt +returnStmt +breakStmt +expression +simpleExpression +orExpression +andExpression +unaryRelExpression +relExpression +relop +sumExpression +sumop +termExpression +mulop +unaryExpression +unaryop +factor +mutable +immutable +call +constant + + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 50, 329, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 4, 18, 9, 18, 4, 19, 9, 19, 4, 20, 9, 20, 4, 21, 9, 21, 4, 22, 9, 22, 4, 23, 9, 23, 4, 24, 9, 24, 4, 25, 9, 25, 4, 26, 9, 26, 4, 27, 9, 27, 4, 28, 9, 28, 4, 29, 9, 29, 4, 30, 9, 30, 4, 31, 9, 31, 4, 32, 9, 32, 4, 33, 9, 33, 4, 34, 9, 34, 3, 2, 6, 2, 70, 10, 2, 13, 2, 14, 2, 71, 3, 3, 3, 3, 5, 3, 76, 10, 3, 3, 4, 3, 4, 3, 4, 3, 4, 7, 4, 82, 10, 4, 12, 4, 14, 4, 85, 11, 4, 3, 4, 3, 4, 3, 5, 3, 5, 3, 5, 3, 5, 3, 5, 5, 5, 94, 10, 5, 3, 6, 3, 6, 5, 6, 98, 10, 6, 3, 6, 3, 6, 3, 6, 5, 6, 103, 10, 6, 3, 6, 3, 6, 7, 6, 107, 10, 6, 12, 6, 14, 6, 110, 11, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 5, 9, 123, 10, 9, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 5, 10, 131, 10, 10, 3, 11, 3, 11, 7, 11, 135, 10, 11, 12, 11, 14, 11, 138, 11, 11, 3, 11, 7, 11, 141, 10, 11, 12, 11, 14, 11, 144, 11, 11, 3, 11, 3, 11, 3, 12, 3, 12, 3, 12, 3, 12, 5, 12, 152, 10, 12, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 3, 13, 5, 13, 168, 10, 13, 3, 14, 3, 14, 3, 14, 3, 14, 3, 14, 3, 14, 3, 15, 3, 15, 3, 15, 3, 15, 3, 15, 3, 15, 5, 15, 182, 10, 15, 3, 16, 3, 16, 3, 16, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 5, 17, 214, 10, 17, 3, 18, 3, 18, 3, 19, 3, 19, 3, 19, 7, 19, 221, 10, 19, 12, 19, 14, 19, 224, 11, 19, 3, 19, 3, 19, 3, 20, 3, 20, 3, 20, 7, 20, 231, 10, 20, 12, 20, 14, 20, 234, 11, 20, 3, 20, 3, 20, 3, 21, 7, 21, 239, 10, 21, 12, 21, 14, 21, 242, 11, 21, 3, 21, 3, 21, 3, 22, 3, 22, 3, 22, 7, 22, 249, 10, 22, 12, 22, 14, 22, 252, 11, 22, 3, 22, 3, 22, 3, 23, 3, 23, 3, 24, 3, 24, 3, 24, 7, 24, 261, 10, 24, 12, 24, 14, 24, 264, 11, 24, 3, 24, 3, 24, 3, 25, 3, 25, 3, 26, 3, 26, 3, 26, 7, 26, 273, 10, 26, 12, 26, 14, 26, 276, 11, 26, 3, 26, 3, 26, 3, 27, 3, 27, 3, 28, 7, 28, 283, 10, 28, 12, 28, 14, 28, 286, 11, 28, 3, 28, 3, 28, 3, 29, 3, 29, 3, 30, 3, 30, 5, 30, 294, 10, 30, 3, 31, 3, 31, 3, 31, 3, 31, 3, 31, 3, 31, 5, 31, 302, 10, 31, 3, 32, 3, 32, 3, 32, 3, 32, 3, 32, 3, 32, 5, 32, 310, 10, 32, 3, 33, 3, 33, 3, 33, 3, 33, 3, 33, 7, 33, 317, 10, 33, 12, 33, 14, 33, 320, 11, 33, 3, 33, 5, 33, 323, 10, 33, 3, 33, 3, 33, 3, 34, 3, 34, 3, 34, 2, 2, 35, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 2, 8, 3, 2, 10, 12, 3, 2, 30, 35, 3, 2, 36, 37, 3, 2, 38, 40, 4, 2, 37, 38, 41, 41, 4, 2, 42, 43, 45, 47, 2, 333, 2, 69, 3, 2, 2, 2, 4, 75, 3, 2, 2, 2, 6, 77, 3, 2, 2, 2, 8, 93, 3, 2, 2, 2, 10, 97, 3, 2, 2, 2, 12, 114, 3, 2, 2, 2, 14, 116, 3, 2, 2, 2, 16, 122, 3, 2, 2, 2, 18, 130, 3, 2, 2, 2, 20, 132, 3, 2, 2, 2, 22, 151, 3, 2, 2, 2, 24, 167, 3, 2, 2, 2, 26, 169, 3, 2, 2, 2, 28, 181, 3, 2, 2, 2, 30, 183, 3, 2, 2, 2, 32, 213, 3, 2, 2, 2, 34, 215, 3, 2, 2, 2, 36, 222, 3, 2, 2, 2, 38, 232, 3, 2, 2, 2, 40, 240, 3, 2, 2, 2, 42, 250, 3, 2, 2, 2, 44, 255, 3, 2, 2, 2, 46, 262, 3, 2, 2, 2, 48, 267, 3, 2, 2, 2, 50, 274, 3, 2, 2, 2, 52, 279, 3, 2, 2, 2, 54, 284, 3, 2, 2, 2, 56, 289, 3, 2, 2, 2, 58, 293, 3, 2, 2, 2, 60, 301, 3, 2, 2, 2, 62, 309, 3, 2, 2, 2, 64, 311, 3, 2, 2, 2, 66, 326, 3, 2, 2, 2, 68, 70, 5, 4, 3, 2, 69, 68, 3, 2, 2, 2, 70, 71, 3, 2, 2, 2, 71, 69, 3, 2, 2, 2, 71, 72, 3, 2, 2, 2, 72, 3, 3, 2, 2, 2, 73, 76, 5, 6, 4, 2, 74, 76, 5, 10, 6, 2, 75, 73, 3, 2, 2, 2, 75, 74, 3, 2, 2, 2, 76, 5, 3, 2, 2, 2, 77, 78, 5, 12, 7, 2, 78, 83, 5, 8, 5, 2, 79, 80, 7, 3, 2, 2, 80, 82, 5, 8, 5, 2, 81, 79, 3, 2, 2, 2, 82, 85, 3, 2, 2, 2, 83, 81, 3, 2, 2, 2, 83, 84, 3, 2, 2, 2, 84, 86, 3, 2, 2, 2, 85, 83, 3, 2, 2, 2, 86, 87, 7, 4, 2, 2, 87, 7, 3, 2, 2, 2, 88, 94, 7, 44, 2, 2, 89, 90, 7, 44, 2, 2, 90, 91, 7, 5, 2, 2, 91, 92, 7, 45, 2, 2, 92, 94, 7, 6, 2, 2, 93, 88, 3, 2, 2, 2, 93, 89, 3, 2, 2, 2, 94, 9, 3, 2, 2, 2, 95, 98, 7, 7, 2, 2, 96, 98, 5, 12, 7, 2, 97, 95, 3, 2, 2, 2, 97, 96, 3, 2, 2, 2, 98, 99, 3, 2, 2, 2, 99, 100, 7, 44, 2, 2, 100, 102, 7, 8, 2, 2, 101, 103, 5, 14, 8, 2, 102, 101, 3, 2, 2, 2, 102, 103, 3, 2, 2, 2, 103, 108, 3, 2, 2, 2, 104, 105, 7, 3, 2, 2, 105, 107, 5, 14, 8, 2, 106, 104, 3, 2, 2, 2, 107, 110, 3, 2, 2, 2, 108, 106, 3, 2, 2, 2, 108, 109, 3, 2, 2, 2, 109, 111, 3, 2, 2, 2, 110, 108, 3, 2, 2, 2, 111, 112, 7, 9, 2, 2, 112, 113, 5, 18, 10, 2, 113, 11, 3, 2, 2, 2, 114, 115, 9, 2, 2, 2, 115, 13, 3, 2, 2, 2, 116, 117, 5, 12, 7, 2, 117, 118, 5, 16, 9, 2, 118, 15, 3, 2, 2, 2, 119, 123, 7, 44, 2, 2, 120, 121, 7, 44, 2, 2, 121, 123, 7, 13, 2, 2, 122, 119, 3, 2, 2, 2, 122, 120, 3, 2, 2, 2, 123, 17, 3, 2, 2, 2, 124, 131, 5, 22, 12, 2, 125, 131, 5, 20, 11, 2, 126, 131, 5, 24, 13, 2, 127, 131, 5, 26, 14, 2, 128, 131, 5, 28, 15, 2, 129, 131, 5, 30, 16, 2, 130, 124, 3, 2, 2, 2, 130, 125, 3, 2, 2, 2, 130, 126, 3, 2, 2, 2, 130, 127, 3, 2, 2, 2, 130, 128, 3, 2, 2, 2, 130, 129, 3, 2, 2, 2, 131, 19, 3, 2, 2, 2, 132, 136, 7, 14, 2, 2, 133, 135, 5, 6, 4, 2, 134, 133, 3, 2, 2, 2, 135, 138, 3, 2, 2, 2, 136, 134, 3, 2, 2, 2, 136, 137, 3, 2, 2, 2, 137, 142, 3, 2, 2, 2, 138, 136, 3, 2, 2, 2, 139, 141, 5, 18, 10, 2, 140, 139, 3, 2, 2, 2, 141, 144, 3, 2, 2, 2, 142, 140, 3, 2, 2, 2, 142, 143, 3, 2, 2, 2, 143, 145, 3, 2, 2, 2, 144, 142, 3, 2, 2, 2, 145, 146, 7, 15, 2, 2, 146, 21, 3, 2, 2, 2, 147, 148, 5, 32, 17, 2, 148, 149, 7, 4, 2, 2, 149, 152, 3, 2, 2, 2, 150, 152, 7, 4, 2, 2, 151, 147, 3, 2, 2, 2, 151, 150, 3, 2, 2, 2, 152, 23, 3, 2, 2, 2, 153, 154, 7, 16, 2, 2, 154, 155, 7, 8, 2, 2, 155, 156, 5, 34, 18, 2, 156, 157, 7, 9, 2, 2, 157, 158, 5, 18, 10, 2, 158, 168, 3, 2, 2, 2, 159, 160, 7, 16, 2, 2, 160, 161, 7, 8, 2, 2, 161, 162, 5, 34, 18, 2, 162, 163, 7, 9, 2, 2, 163, 164, 5, 18, 10, 2, 164, 165, 7, 17, 2, 2, 165, 166, 5, 18, 10, 2, 166, 168, 3, 2, 2, 2, 167, 153, 3, 2, 2, 2, 167, 159, 3, 2, 2, 2, 168, 25, 3, 2, 2, 2, 169, 170, 7, 18, 2, 2, 170, 171, 7, 8, 2, 2, 171, 172, 5, 34, 18, 2, 172, 173, 7, 9, 2, 2, 173, 174, 5, 18, 10, 2, 174, 27, 3, 2, 2, 2, 175, 176, 7, 19, 2, 2, 176, 182, 7, 4, 2, 2, 177, 178, 7, 19, 2, 2, 178, 179, 5, 32, 17, 2, 179, 180, 7, 4, 2, 2, 180, 182, 3, 2, 2, 2, 181, 175, 3, 2, 2, 2, 181, 177, 3, 2, 2, 2, 182, 29, 3, 2, 2, 2, 183, 184, 7, 20, 2, 2, 184, 185, 7, 4, 2, 2, 185, 31, 3, 2, 2, 2, 186, 187, 5, 60, 31, 2, 187, 188, 7, 21, 2, 2, 188, 189, 5, 32, 17, 2, 189, 214, 3, 2, 2, 2, 190, 191, 5, 60, 31, 2, 191, 192, 7, 22, 2, 2, 192, 193, 5, 32, 17, 2, 193, 214, 3, 2, 2, 2, 194, 195, 5, 60, 31, 2, 195, 196, 7, 23, 2, 2, 196, 197, 5, 32, 17, 2, 197, 214, 3, 2, 2, 2, 198, 199, 5, 60, 31, 2, 199, 200, 7, 24, 2, 2, 200, 201, 5, 32, 17, 2, 201, 214, 3, 2, 2, 2, 202, 203, 5, 60, 31, 2, 203, 204, 7, 25, 2, 2, 204, 205, 5, 32, 17, 2, 205, 214, 3, 2, 2, 2, 206, 207, 5, 60, 31, 2, 207, 208, 7, 26, 2, 2, 208, 214, 3, 2, 2, 2, 209, 210, 5, 60, 31, 2, 210, 211, 7, 27, 2, 2, 211, 214, 3, 2, 2, 2, 212, 214, 5, 34, 18, 2, 213, 186, 3, 2, 2, 2, 213, 190, 3, 2, 2, 2, 213, 194, 3, 2, 2, 2, 213, 198, 3, 2, 2, 2, 213, 202, 3, 2, 2, 2, 213, 206, 3, 2, 2, 2, 213, 209, 3, 2, 2, 2, 213, 212, 3, 2, 2, 2, 214, 33, 3, 2, 2, 2, 215, 216, 5, 36, 19, 2, 216, 35, 3, 2, 2, 2, 217, 218, 5, 38, 20, 2, 218, 219, 7, 28, 2, 2, 219, 221, 3, 2, 2, 2, 220, 217, 3, 2, 2, 2, 221, 224, 3, 2, 2, 2, 222, 220, 3, 2, 2, 2, 222, 223, 3, 2, 2, 2, 223, 225, 3, 2, 2, 2, 224, 222, 3, 2, 2, 2, 225, 226, 5, 38, 20, 2, 226, 37, 3, 2, 2, 2, 227, 228, 5, 40, 21, 2, 228, 229, 7, 29, 2, 2, 229, 231, 3, 2, 2, 2, 230, 227, 3, 2, 2, 2, 231, 234, 3, 2, 2, 2, 232, 230, 3, 2, 2, 2, 232, 233, 3, 2, 2, 2, 233, 235, 3, 2, 2, 2, 234, 232, 3, 2, 2, 2, 235, 236, 5, 40, 21, 2, 236, 39, 3, 2, 2, 2, 237, 239, 7, 48, 2, 2, 238, 237, 3, 2, 2, 2, 239, 242, 3, 2, 2, 2, 240, 238, 3, 2, 2, 2, 240, 241, 3, 2, 2, 2, 241, 243, 3, 2, 2, 2, 242, 240, 3, 2, 2, 2, 243, 244, 5, 42, 22, 2, 244, 41, 3, 2, 2, 2, 245, 246, 5, 46, 24, 2, 246, 247, 5, 44, 23, 2, 247, 249, 3, 2, 2, 2, 248, 245, 3, 2, 2, 2, 249, 252, 3, 2, 2, 2, 250, 248, 3, 2, 2, 2, 250, 251, 3, 2, 2, 2, 251, 253, 3, 2, 2, 2, 252, 250, 3, 2, 2, 2, 253, 254, 5, 46, 24, 2, 254, 43, 3, 2, 2, 2, 255, 256, 9, 3, 2, 2, 256, 45, 3, 2, 2, 2, 257, 258, 5, 50, 26, 2, 258, 259, 5, 48, 25, 2, 259, 261, 3, 2, 2, 2, 260, 257, 3, 2, 2, 2, 261, 264, 3, 2, 2, 2, 262, 260, 3, 2, 2, 2, 262, 263, 3, 2, 2, 2, 263, 265, 3, 2, 2, 2, 264, 262, 3, 2, 2, 2, 265, 266, 5, 50, 26, 2, 266, 47, 3, 2, 2, 2, 267, 268, 9, 4, 2, 2, 268, 49, 3, 2, 2, 2, 269, 270, 5, 54, 28, 2, 270, 271, 5, 52, 27, 2, 271, 273, 3, 2, 2, 2, 272, 269, 3, 2, 2, 2, 273, 276, 3, 2, 2, 2, 274, 272, 3, 2, 2, 2, 274, 275, 3, 2, 2, 2, 275, 277, 3, 2, 2, 2, 276, 274, 3, 2, 2, 2, 277, 278, 5, 54, 28, 2, 278, 51, 3, 2, 2, 2, 279, 280, 9, 5, 2, 2, 280, 53, 3, 2, 2, 2, 281, 283, 5, 56, 29, 2, 282, 281, 3, 2, 2, 2, 283, 286, 3, 2, 2, 2, 284, 282, 3, 2, 2, 2, 284, 285, 3, 2, 2, 2, 285, 287, 3, 2, 2, 2, 286, 284, 3, 2, 2, 2, 287, 288, 5, 58, 30, 2, 288, 55, 3, 2, 2, 2, 289, 290, 9, 6, 2, 2, 290, 57, 3, 2, 2, 2, 291, 294, 5, 62, 32, 2, 292, 294, 5, 60, 31, 2, 293, 291, 3, 2, 2, 2, 293, 292, 3, 2, 2, 2, 294, 59, 3, 2, 2, 2, 295, 302, 7, 44, 2, 2, 296, 297, 7, 44, 2, 2, 297, 298, 7, 5, 2, 2, 298, 299, 5, 32, 17, 2, 299, 300, 7, 6, 2, 2, 300, 302, 3, 2, 2, 2, 301, 295, 3, 2, 2, 2, 301, 296, 3, 2, 2, 2, 302, 61, 3, 2, 2, 2, 303, 304, 7, 8, 2, 2, 304, 305, 5, 32, 17, 2, 305, 306, 7, 9, 2, 2, 306, 310, 3, 2, 2, 2, 307, 310, 5, 64, 33, 2, 308, 310, 5, 66, 34, 2, 309, 303, 3, 2, 2, 2, 309, 307, 3, 2, 2, 2, 309, 308, 3, 2, 2, 2, 310, 63, 3, 2, 2, 2, 311, 312, 7, 44, 2, 2, 312, 318, 7, 8, 2, 2, 313, 314, 5, 32, 17, 2, 314, 315, 7, 3, 2, 2, 315, 317, 3, 2, 2, 2, 316, 313, 3, 2, 2, 2, 317, 320, 3, 2, 2, 2, 318, 316, 3, 2, 2, 2, 318, 319, 3, 2, 2, 2, 319, 322, 3, 2, 2, 2, 320, 318, 3, 2, 2, 2, 321, 323, 5, 32, 17, 2, 322, 321, 3, 2, 2, 2, 322, 323, 3, 2, 2, 2, 323, 324, 3, 2, 2, 2, 324, 325, 7, 9, 2, 2, 325, 65, 3, 2, 2, 2, 326, 327, 9, 7, 2, 2, 327, 67, 3, 2, 2, 2, 29, 71, 75, 83, 93, 97, 102, 108, 122, 130, 136, 142, 151, 167, 181, 213, 222, 232, 240, 250, 262, 274, 284, 293, 301, 309, 318, 322] \ No newline at end of file diff --git a/parser/Cminus.tokens b/parser/Cminus.tokens new file mode 100644 index 0000000..ae5a5c0 --- /dev/null +++ b/parser/Cminus.tokens @@ -0,0 +1,90 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +T__5=6 +T__6=7 +T__7=8 +T__8=9 +T__9=10 +T__10=11 +T__11=12 +T__12=13 +T__13=14 +T__14=15 +T__15=16 +T__16=17 +T__17=18 +T__18=19 +T__19=20 +T__20=21 +T__21=22 +T__22=23 +T__23=24 +T__24=25 +T__25=26 +T__26=27 +T__27=28 +T__28=29 +T__29=30 +T__30=31 +T__31=32 +T__32=33 +T__33=34 +T__34=35 +T__35=36 +T__36=37 +T__37=38 +T__38=39 +T__39=40 +T__40=41 +ID=42 +NUMCONST=43 +STRINGCONST=44 +CHARCONST=45 +BANG=46 +WS=47 +COMMENT=48 +','=1 +';'=2 +'['=3 +']'=4 +'void'=5 +'('=6 +')'=7 +'int'=8 +'bool'=9 +'char'=10 +'[]'=11 +'{'=12 +'}'=13 +'if'=14 +'else'=15 +'while'=16 +'return'=17 +'break'=18 +'='=19 +'+='=20 +'-='=21 +'*='=22 +'/='=23 +'++'=24 +'--'=25 +'||'=26 +'&&'=27 +'<='=28 +'<'=29 +'>'=30 +'>='=31 +'=='=32 +'!='=33 +'+'=34 +'-'=35 +'*'=36 +'/'=37 +'%'=38 +'?'=39 +'true'=40 +'false'=41 +'!'=46 diff --git a/parser/CminusBaseListener.java b/parser/CminusBaseListener.java new file mode 100644 index 0000000..65a3493 --- /dev/null +++ b/parser/CminusBaseListener.java @@ -0,0 +1,435 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; + +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.tree.ErrorNode; +import org.antlr.v4.runtime.tree.TerminalNode; + +/** + * This class provides an empty implementation of {@link CminusListener}, + * which can be extended to create a listener which only needs to handle a subset + * of the available methods. + */ +public class CminusBaseListener implements CminusListener { + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterProgram(CminusParser.ProgramContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitProgram(CminusParser.ProgramContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterDeclaration(CminusParser.DeclarationContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitDeclaration(CminusParser.DeclarationContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterVarDeclaration(CminusParser.VarDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitVarDeclaration(CminusParser.VarDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterVarDeclId(CminusParser.VarDeclIdContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitVarDeclId(CminusParser.VarDeclIdContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterFunDeclaration(CminusParser.FunDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitFunDeclaration(CminusParser.FunDeclarationContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterParam(CminusParser.ParamContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitParam(CminusParser.ParamContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterParamId(CminusParser.ParamIdContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitParamId(CminusParser.ParamIdContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterStatement(CminusParser.StatementContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitStatement(CminusParser.StatementContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterCompoundStmt(CminusParser.CompoundStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitCompoundStmt(CminusParser.CompoundStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterExpressionStmt(CminusParser.ExpressionStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterIfStmt(CminusParser.IfStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitIfStmt(CminusParser.IfStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterWhileStmt(CminusParser.WhileStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitWhileStmt(CminusParser.WhileStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterReturnStmt(CminusParser.ReturnStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitReturnStmt(CminusParser.ReturnStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterBreakStmt(CminusParser.BreakStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitBreakStmt(CminusParser.BreakStmtContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterExpression(CminusParser.ExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitExpression(CminusParser.ExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterSimpleExpression(CminusParser.SimpleExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitSimpleExpression(CminusParser.SimpleExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterOrExpression(CminusParser.OrExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitOrExpression(CminusParser.OrExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterAndExpression(CminusParser.AndExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitAndExpression(CminusParser.AndExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterRelExpression(CminusParser.RelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitRelExpression(CminusParser.RelExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterRelop(CminusParser.RelopContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitRelop(CminusParser.RelopContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterSumExpression(CminusParser.SumExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitSumExpression(CminusParser.SumExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterSumop(CminusParser.SumopContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitSumop(CminusParser.SumopContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterTermExpression(CminusParser.TermExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitTermExpression(CminusParser.TermExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterMulop(CminusParser.MulopContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitMulop(CminusParser.MulopContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterUnaryExpression(CminusParser.UnaryExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterUnaryop(CminusParser.UnaryopContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitUnaryop(CminusParser.UnaryopContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterFactor(CminusParser.FactorContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitFactor(CminusParser.FactorContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterMutable(CminusParser.MutableContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitMutable(CminusParser.MutableContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterImmutable(CminusParser.ImmutableContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitImmutable(CminusParser.ImmutableContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterCall(CminusParser.CallContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitCall(CminusParser.CallContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterConstant(CminusParser.ConstantContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitConstant(CminusParser.ConstantContext ctx) { } + + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void visitTerminal(TerminalNode node) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void visitErrorNode(ErrorNode node) { } +} \ No newline at end of file diff --git a/parser/CminusBaseVisitor.java b/parser/CminusBaseVisitor.java new file mode 100644 index 0000000..150f316 --- /dev/null +++ b/parser/CminusBaseVisitor.java @@ -0,0 +1,245 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; + +/** + * This class provides an empty implementation of {@link CminusVisitor}, + * which can be extended to create a visitor which only needs to handle a subset + * of the available methods. + * + * @param The return type of the visit operation. Use {@link Void} for + * operations with no return type. + */ +public class CminusBaseVisitor extends AbstractParseTreeVisitor implements CminusVisitor { + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitProgram(CminusParser.ProgramContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitDeclaration(CminusParser.DeclarationContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitVarDeclaration(CminusParser.VarDeclarationContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitVarDeclId(CminusParser.VarDeclIdContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitFunDeclaration(CminusParser.FunDeclarationContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitParam(CminusParser.ParamContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitParamId(CminusParser.ParamIdContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitStatement(CminusParser.StatementContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitCompoundStmt(CminusParser.CompoundStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitIfStmt(CminusParser.IfStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitWhileStmt(CminusParser.WhileStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitReturnStmt(CminusParser.ReturnStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitBreakStmt(CminusParser.BreakStmtContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitExpression(CminusParser.ExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitSimpleExpression(CminusParser.SimpleExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitOrExpression(CminusParser.OrExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitAndExpression(CminusParser.AndExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitRelExpression(CminusParser.RelExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitRelop(CminusParser.RelopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitSumExpression(CminusParser.SumExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitSumop(CminusParser.SumopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitTermExpression(CminusParser.TermExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitMulop(CminusParser.MulopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitUnaryop(CminusParser.UnaryopContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitFactor(CminusParser.FactorContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitMutable(CminusParser.MutableContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitImmutable(CminusParser.ImmutableContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitCall(CminusParser.CallContext ctx) { return visitChildren(ctx); } + /** + * {@inheritDoc} + * + *

The default implementation returns the result of calling + * {@link #visitChildren} on {@code ctx}.

+ */ + @Override public T visitConstant(CminusParser.ConstantContext ctx) { return visitChildren(ctx); } +} \ No newline at end of file diff --git a/parser/CminusLexer.interp b/parser/CminusLexer.interp new file mode 100644 index 0000000..a9dc30a --- /dev/null +++ b/parser/CminusLexer.interp @@ -0,0 +1,163 @@ +token literal names: +null +',' +';' +'[' +']' +'void' +'(' +')' +'int' +'bool' +'char' +'[]' +'{' +'}' +'if' +'else' +'while' +'return' +'break' +'=' +'+=' +'-=' +'*=' +'/=' +'++' +'--' +'||' +'&&' +'<=' +'<' +'>' +'>=' +'==' +'!=' +'+' +'-' +'*' +'/' +'%' +'?' +'true' +'false' +null +null +null +null +'!' +null +null + +token symbolic names: +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +null +ID +NUMCONST +STRINGCONST +CHARCONST +BANG +WS +COMMENT + +rule names: +T__0 +T__1 +T__2 +T__3 +T__4 +T__5 +T__6 +T__7 +T__8 +T__9 +T__10 +T__11 +T__12 +T__13 +T__14 +T__15 +T__16 +T__17 +T__18 +T__19 +T__20 +T__21 +T__22 +T__23 +T__24 +T__25 +T__26 +T__27 +T__28 +T__29 +T__30 +T__31 +T__32 +T__33 +T__34 +T__35 +T__36 +T__37 +T__38 +T__39 +T__40 +ID +NUMCONST +STRINGCONST +CHARCONST +BANG +WS +COMMENT +LETTER +DIGIT + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 50, 306, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 4, 18, 9, 18, 4, 19, 9, 19, 4, 20, 9, 20, 4, 21, 9, 21, 4, 22, 9, 22, 4, 23, 9, 23, 4, 24, 9, 24, 4, 25, 9, 25, 4, 26, 9, 26, 4, 27, 9, 27, 4, 28, 9, 28, 4, 29, 9, 29, 4, 30, 9, 30, 4, 31, 9, 31, 4, 32, 9, 32, 4, 33, 9, 33, 4, 34, 9, 34, 4, 35, 9, 35, 4, 36, 9, 36, 4, 37, 9, 37, 4, 38, 9, 38, 4, 39, 9, 39, 4, 40, 9, 40, 4, 41, 9, 41, 4, 42, 9, 42, 4, 43, 9, 43, 4, 44, 9, 44, 4, 45, 9, 45, 4, 46, 9, 46, 4, 47, 9, 47, 4, 48, 9, 48, 4, 49, 9, 49, 4, 50, 9, 50, 4, 51, 9, 51, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 3, 9, 3, 9, 3, 9, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 3, 12, 3, 12, 3, 12, 3, 13, 3, 13, 3, 14, 3, 14, 3, 15, 3, 15, 3, 15, 3, 16, 3, 16, 3, 16, 3, 16, 3, 16, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 18, 3, 18, 3, 18, 3, 18, 3, 18, 3, 18, 3, 18, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 19, 3, 20, 3, 20, 3, 21, 3, 21, 3, 21, 3, 22, 3, 22, 3, 22, 3, 23, 3, 23, 3, 23, 3, 24, 3, 24, 3, 24, 3, 25, 3, 25, 3, 25, 3, 26, 3, 26, 3, 26, 3, 27, 3, 27, 3, 27, 3, 28, 3, 28, 3, 28, 3, 29, 3, 29, 3, 29, 3, 30, 3, 30, 3, 31, 3, 31, 3, 32, 3, 32, 3, 32, 3, 33, 3, 33, 3, 33, 3, 34, 3, 34, 3, 34, 3, 35, 3, 35, 3, 36, 3, 36, 3, 37, 3, 37, 3, 38, 3, 38, 3, 39, 3, 39, 3, 40, 3, 40, 3, 41, 3, 41, 3, 41, 3, 41, 3, 41, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 43, 3, 43, 3, 43, 7, 43, 237, 10, 43, 12, 43, 14, 43, 240, 11, 43, 3, 44, 6, 44, 243, 10, 44, 13, 44, 14, 44, 244, 3, 45, 3, 45, 3, 45, 3, 45, 7, 45, 251, 10, 45, 12, 45, 14, 45, 254, 11, 45, 3, 45, 3, 45, 3, 46, 3, 46, 3, 46, 3, 46, 5, 46, 262, 10, 46, 3, 46, 3, 46, 3, 47, 3, 47, 3, 48, 6, 48, 269, 10, 48, 13, 48, 14, 48, 270, 3, 48, 3, 48, 3, 49, 3, 49, 3, 49, 3, 49, 7, 49, 279, 10, 49, 12, 49, 14, 49, 282, 11, 49, 3, 49, 5, 49, 285, 10, 49, 3, 49, 3, 49, 3, 49, 3, 49, 3, 49, 7, 49, 292, 10, 49, 12, 49, 14, 49, 295, 11, 49, 3, 49, 3, 49, 5, 49, 299, 10, 49, 3, 49, 3, 49, 3, 50, 3, 50, 3, 51, 3, 51, 4, 252, 293, 2, 52, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 29, 16, 31, 17, 33, 18, 35, 19, 37, 20, 39, 21, 41, 22, 43, 23, 45, 24, 47, 25, 49, 26, 51, 27, 53, 28, 55, 29, 57, 30, 59, 31, 61, 32, 63, 33, 65, 34, 67, 35, 69, 36, 71, 37, 73, 38, 75, 39, 77, 40, 79, 41, 81, 42, 83, 43, 85, 44, 87, 45, 89, 46, 91, 47, 93, 48, 95, 49, 97, 50, 99, 2, 101, 2, 3, 2, 6, 3, 2, 36, 36, 5, 2, 11, 12, 14, 15, 34, 34, 4, 2, 12, 12, 15, 15, 4, 2, 67, 92, 99, 124, 2, 314, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2, 2, 2, 2, 29, 3, 2, 2, 2, 2, 31, 3, 2, 2, 2, 2, 33, 3, 2, 2, 2, 2, 35, 3, 2, 2, 2, 2, 37, 3, 2, 2, 2, 2, 39, 3, 2, 2, 2, 2, 41, 3, 2, 2, 2, 2, 43, 3, 2, 2, 2, 2, 45, 3, 2, 2, 2, 2, 47, 3, 2, 2, 2, 2, 49, 3, 2, 2, 2, 2, 51, 3, 2, 2, 2, 2, 53, 3, 2, 2, 2, 2, 55, 3, 2, 2, 2, 2, 57, 3, 2, 2, 2, 2, 59, 3, 2, 2, 2, 2, 61, 3, 2, 2, 2, 2, 63, 3, 2, 2, 2, 2, 65, 3, 2, 2, 2, 2, 67, 3, 2, 2, 2, 2, 69, 3, 2, 2, 2, 2, 71, 3, 2, 2, 2, 2, 73, 3, 2, 2, 2, 2, 75, 3, 2, 2, 2, 2, 77, 3, 2, 2, 2, 2, 79, 3, 2, 2, 2, 2, 81, 3, 2, 2, 2, 2, 83, 3, 2, 2, 2, 2, 85, 3, 2, 2, 2, 2, 87, 3, 2, 2, 2, 2, 89, 3, 2, 2, 2, 2, 91, 3, 2, 2, 2, 2, 93, 3, 2, 2, 2, 2, 95, 3, 2, 2, 2, 2, 97, 3, 2, 2, 2, 3, 103, 3, 2, 2, 2, 5, 105, 3, 2, 2, 2, 7, 107, 3, 2, 2, 2, 9, 109, 3, 2, 2, 2, 11, 111, 3, 2, 2, 2, 13, 116, 3, 2, 2, 2, 15, 118, 3, 2, 2, 2, 17, 120, 3, 2, 2, 2, 19, 124, 3, 2, 2, 2, 21, 129, 3, 2, 2, 2, 23, 134, 3, 2, 2, 2, 25, 137, 3, 2, 2, 2, 27, 139, 3, 2, 2, 2, 29, 141, 3, 2, 2, 2, 31, 144, 3, 2, 2, 2, 33, 149, 3, 2, 2, 2, 35, 155, 3, 2, 2, 2, 37, 162, 3, 2, 2, 2, 39, 168, 3, 2, 2, 2, 41, 170, 3, 2, 2, 2, 43, 173, 3, 2, 2, 2, 45, 176, 3, 2, 2, 2, 47, 179, 3, 2, 2, 2, 49, 182, 3, 2, 2, 2, 51, 185, 3, 2, 2, 2, 53, 188, 3, 2, 2, 2, 55, 191, 3, 2, 2, 2, 57, 194, 3, 2, 2, 2, 59, 197, 3, 2, 2, 2, 61, 199, 3, 2, 2, 2, 63, 201, 3, 2, 2, 2, 65, 204, 3, 2, 2, 2, 67, 207, 3, 2, 2, 2, 69, 210, 3, 2, 2, 2, 71, 212, 3, 2, 2, 2, 73, 214, 3, 2, 2, 2, 75, 216, 3, 2, 2, 2, 77, 218, 3, 2, 2, 2, 79, 220, 3, 2, 2, 2, 81, 222, 3, 2, 2, 2, 83, 227, 3, 2, 2, 2, 85, 233, 3, 2, 2, 2, 87, 242, 3, 2, 2, 2, 89, 246, 3, 2, 2, 2, 91, 257, 3, 2, 2, 2, 93, 265, 3, 2, 2, 2, 95, 268, 3, 2, 2, 2, 97, 298, 3, 2, 2, 2, 99, 302, 3, 2, 2, 2, 101, 304, 3, 2, 2, 2, 103, 104, 7, 46, 2, 2, 104, 4, 3, 2, 2, 2, 105, 106, 7, 61, 2, 2, 106, 6, 3, 2, 2, 2, 107, 108, 7, 93, 2, 2, 108, 8, 3, 2, 2, 2, 109, 110, 7, 95, 2, 2, 110, 10, 3, 2, 2, 2, 111, 112, 7, 120, 2, 2, 112, 113, 7, 113, 2, 2, 113, 114, 7, 107, 2, 2, 114, 115, 7, 102, 2, 2, 115, 12, 3, 2, 2, 2, 116, 117, 7, 42, 2, 2, 117, 14, 3, 2, 2, 2, 118, 119, 7, 43, 2, 2, 119, 16, 3, 2, 2, 2, 120, 121, 7, 107, 2, 2, 121, 122, 7, 112, 2, 2, 122, 123, 7, 118, 2, 2, 123, 18, 3, 2, 2, 2, 124, 125, 7, 100, 2, 2, 125, 126, 7, 113, 2, 2, 126, 127, 7, 113, 2, 2, 127, 128, 7, 110, 2, 2, 128, 20, 3, 2, 2, 2, 129, 130, 7, 101, 2, 2, 130, 131, 7, 106, 2, 2, 131, 132, 7, 99, 2, 2, 132, 133, 7, 116, 2, 2, 133, 22, 3, 2, 2, 2, 134, 135, 7, 93, 2, 2, 135, 136, 7, 95, 2, 2, 136, 24, 3, 2, 2, 2, 137, 138, 7, 125, 2, 2, 138, 26, 3, 2, 2, 2, 139, 140, 7, 127, 2, 2, 140, 28, 3, 2, 2, 2, 141, 142, 7, 107, 2, 2, 142, 143, 7, 104, 2, 2, 143, 30, 3, 2, 2, 2, 144, 145, 7, 103, 2, 2, 145, 146, 7, 110, 2, 2, 146, 147, 7, 117, 2, 2, 147, 148, 7, 103, 2, 2, 148, 32, 3, 2, 2, 2, 149, 150, 7, 121, 2, 2, 150, 151, 7, 106, 2, 2, 151, 152, 7, 107, 2, 2, 152, 153, 7, 110, 2, 2, 153, 154, 7, 103, 2, 2, 154, 34, 3, 2, 2, 2, 155, 156, 7, 116, 2, 2, 156, 157, 7, 103, 2, 2, 157, 158, 7, 118, 2, 2, 158, 159, 7, 119, 2, 2, 159, 160, 7, 116, 2, 2, 160, 161, 7, 112, 2, 2, 161, 36, 3, 2, 2, 2, 162, 163, 7, 100, 2, 2, 163, 164, 7, 116, 2, 2, 164, 165, 7, 103, 2, 2, 165, 166, 7, 99, 2, 2, 166, 167, 7, 109, 2, 2, 167, 38, 3, 2, 2, 2, 168, 169, 7, 63, 2, 2, 169, 40, 3, 2, 2, 2, 170, 171, 7, 45, 2, 2, 171, 172, 7, 63, 2, 2, 172, 42, 3, 2, 2, 2, 173, 174, 7, 47, 2, 2, 174, 175, 7, 63, 2, 2, 175, 44, 3, 2, 2, 2, 176, 177, 7, 44, 2, 2, 177, 178, 7, 63, 2, 2, 178, 46, 3, 2, 2, 2, 179, 180, 7, 49, 2, 2, 180, 181, 7, 63, 2, 2, 181, 48, 3, 2, 2, 2, 182, 183, 7, 45, 2, 2, 183, 184, 7, 45, 2, 2, 184, 50, 3, 2, 2, 2, 185, 186, 7, 47, 2, 2, 186, 187, 7, 47, 2, 2, 187, 52, 3, 2, 2, 2, 188, 189, 7, 126, 2, 2, 189, 190, 7, 126, 2, 2, 190, 54, 3, 2, 2, 2, 191, 192, 7, 40, 2, 2, 192, 193, 7, 40, 2, 2, 193, 56, 3, 2, 2, 2, 194, 195, 7, 62, 2, 2, 195, 196, 7, 63, 2, 2, 196, 58, 3, 2, 2, 2, 197, 198, 7, 62, 2, 2, 198, 60, 3, 2, 2, 2, 199, 200, 7, 64, 2, 2, 200, 62, 3, 2, 2, 2, 201, 202, 7, 64, 2, 2, 202, 203, 7, 63, 2, 2, 203, 64, 3, 2, 2, 2, 204, 205, 7, 63, 2, 2, 205, 206, 7, 63, 2, 2, 206, 66, 3, 2, 2, 2, 207, 208, 7, 35, 2, 2, 208, 209, 7, 63, 2, 2, 209, 68, 3, 2, 2, 2, 210, 211, 7, 45, 2, 2, 211, 70, 3, 2, 2, 2, 212, 213, 7, 47, 2, 2, 213, 72, 3, 2, 2, 2, 214, 215, 7, 44, 2, 2, 215, 74, 3, 2, 2, 2, 216, 217, 7, 49, 2, 2, 217, 76, 3, 2, 2, 2, 218, 219, 7, 39, 2, 2, 219, 78, 3, 2, 2, 2, 220, 221, 7, 65, 2, 2, 221, 80, 3, 2, 2, 2, 222, 223, 7, 118, 2, 2, 223, 224, 7, 116, 2, 2, 224, 225, 7, 119, 2, 2, 225, 226, 7, 103, 2, 2, 226, 82, 3, 2, 2, 2, 227, 228, 7, 104, 2, 2, 228, 229, 7, 99, 2, 2, 229, 230, 7, 110, 2, 2, 230, 231, 7, 117, 2, 2, 231, 232, 7, 103, 2, 2, 232, 84, 3, 2, 2, 2, 233, 238, 5, 99, 50, 2, 234, 237, 5, 99, 50, 2, 235, 237, 5, 101, 51, 2, 236, 234, 3, 2, 2, 2, 236, 235, 3, 2, 2, 2, 237, 240, 3, 2, 2, 2, 238, 236, 3, 2, 2, 2, 238, 239, 3, 2, 2, 2, 239, 86, 3, 2, 2, 2, 240, 238, 3, 2, 2, 2, 241, 243, 5, 101, 51, 2, 242, 241, 3, 2, 2, 2, 243, 244, 3, 2, 2, 2, 244, 242, 3, 2, 2, 2, 244, 245, 3, 2, 2, 2, 245, 88, 3, 2, 2, 2, 246, 252, 7, 36, 2, 2, 247, 248, 7, 94, 2, 2, 248, 251, 7, 36, 2, 2, 249, 251, 10, 2, 2, 2, 250, 247, 3, 2, 2, 2, 250, 249, 3, 2, 2, 2, 251, 254, 3, 2, 2, 2, 252, 253, 3, 2, 2, 2, 252, 250, 3, 2, 2, 2, 253, 255, 3, 2, 2, 2, 254, 252, 3, 2, 2, 2, 255, 256, 7, 36, 2, 2, 256, 90, 3, 2, 2, 2, 257, 261, 7, 36, 2, 2, 258, 259, 7, 94, 2, 2, 259, 262, 7, 36, 2, 2, 260, 262, 10, 2, 2, 2, 261, 258, 3, 2, 2, 2, 261, 260, 3, 2, 2, 2, 262, 263, 3, 2, 2, 2, 263, 264, 7, 36, 2, 2, 264, 92, 3, 2, 2, 2, 265, 266, 7, 35, 2, 2, 266, 94, 3, 2, 2, 2, 267, 269, 9, 3, 2, 2, 268, 267, 3, 2, 2, 2, 269, 270, 3, 2, 2, 2, 270, 268, 3, 2, 2, 2, 270, 271, 3, 2, 2, 2, 271, 272, 3, 2, 2, 2, 272, 273, 8, 48, 2, 2, 273, 96, 3, 2, 2, 2, 274, 275, 7, 49, 2, 2, 275, 276, 7, 49, 2, 2, 276, 280, 3, 2, 2, 2, 277, 279, 10, 4, 2, 2, 278, 277, 3, 2, 2, 2, 279, 282, 3, 2, 2, 2, 280, 278, 3, 2, 2, 2, 280, 281, 3, 2, 2, 2, 281, 284, 3, 2, 2, 2, 282, 280, 3, 2, 2, 2, 283, 285, 7, 15, 2, 2, 284, 283, 3, 2, 2, 2, 284, 285, 3, 2, 2, 2, 285, 286, 3, 2, 2, 2, 286, 299, 7, 12, 2, 2, 287, 288, 7, 49, 2, 2, 288, 289, 7, 44, 2, 2, 289, 293, 3, 2, 2, 2, 290, 292, 11, 2, 2, 2, 291, 290, 3, 2, 2, 2, 292, 295, 3, 2, 2, 2, 293, 294, 3, 2, 2, 2, 293, 291, 3, 2, 2, 2, 294, 296, 3, 2, 2, 2, 295, 293, 3, 2, 2, 2, 296, 297, 7, 44, 2, 2, 297, 299, 7, 49, 2, 2, 298, 274, 3, 2, 2, 2, 298, 287, 3, 2, 2, 2, 299, 300, 3, 2, 2, 2, 300, 301, 8, 49, 2, 2, 301, 98, 3, 2, 2, 2, 302, 303, 9, 5, 2, 2, 303, 100, 3, 2, 2, 2, 304, 305, 4, 50, 59, 2, 305, 102, 3, 2, 2, 2, 14, 2, 236, 238, 244, 250, 252, 261, 270, 280, 284, 293, 298, 3, 8, 2, 2] \ No newline at end of file diff --git a/parser/CminusLexer.java b/parser/CminusLexer.java new file mode 100644 index 0000000..8224ed8 --- /dev/null +++ b/parser/CminusLexer.java @@ -0,0 +1,232 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class CminusLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9, + T__9=10, T__10=11, T__11=12, T__12=13, T__13=14, T__14=15, T__15=16, T__16=17, + T__17=18, T__18=19, T__19=20, T__20=21, T__21=22, T__22=23, T__23=24, + T__24=25, T__25=26, T__26=27, T__27=28, T__28=29, T__29=30, T__30=31, + T__31=32, T__32=33, T__33=34, T__34=35, T__35=36, T__36=37, T__37=38, + T__38=39, T__39=40, T__40=41, ID=42, NUMCONST=43, STRINGCONST=44, CHARCONST=45, + BANG=46, WS=47, COMMENT=48; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + private static String[] makeRuleNames() { + return new String[] { + "T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "T__7", "T__8", + "T__9", "T__10", "T__11", "T__12", "T__13", "T__14", "T__15", "T__16", + "T__17", "T__18", "T__19", "T__20", "T__21", "T__22", "T__23", "T__24", + "T__25", "T__26", "T__27", "T__28", "T__29", "T__30", "T__31", "T__32", + "T__33", "T__34", "T__35", "T__36", "T__37", "T__38", "T__39", "T__40", + "ID", "NUMCONST", "STRINGCONST", "CHARCONST", "BANG", "WS", "COMMENT", + "LETTER", "DIGIT" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "','", "';'", "'['", "']'", "'void'", "'('", "')'", "'int'", "'bool'", + "'char'", "'[]'", "'{'", "'}'", "'if'", "'else'", "'while'", "'return'", + "'break'", "'='", "'+='", "'-='", "'*='", "'/='", "'++'", "'--'", "'||'", + "'&&'", "'<='", "'<'", "'>'", "'>='", "'=='", "'!='", "'+'", "'-'", "'*'", + "'/'", "'%'", "'?'", "'true'", "'false'", null, null, null, null, "'!'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, "ID", "NUMCONST", "STRINGCONST", + "CHARCONST", "BANG", "WS", "COMMENT" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + + public CminusLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "Cminus.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public String[] getChannelNames() { return channelNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\62\u0132\b\1\4\2"+ + "\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4"+ + "\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22"+ + "\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31"+ + "\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t"+ + " \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t"+ + "+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\3\2"+ + "\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6\3\6\3\6\3\6\3\7\3\7\3\b\3\b\3\t\3"+ + "\t\3\t\3\t\3\n\3\n\3\n\3\n\3\n\3\13\3\13\3\13\3\13\3\13\3\f\3\f\3\f\3"+ + "\r\3\r\3\16\3\16\3\17\3\17\3\17\3\20\3\20\3\20\3\20\3\20\3\21\3\21\3\21"+ + "\3\21\3\21\3\21\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\23\3\23\3\23\3\23"+ + "\3\23\3\23\3\24\3\24\3\25\3\25\3\25\3\26\3\26\3\26\3\27\3\27\3\27\3\30"+ + "\3\30\3\30\3\31\3\31\3\31\3\32\3\32\3\32\3\33\3\33\3\33\3\34\3\34\3\34"+ + "\3\35\3\35\3\35\3\36\3\36\3\37\3\37\3 \3 \3 \3!\3!\3!\3\"\3\"\3\"\3#\3"+ + "#\3$\3$\3%\3%\3&\3&\3\'\3\'\3(\3(\3)\3)\3)\3)\3)\3*\3*\3*\3*\3*\3*\3+"+ + "\3+\3+\7+\u00ed\n+\f+\16+\u00f0\13+\3,\6,\u00f3\n,\r,\16,\u00f4\3-\3-"+ + "\3-\3-\7-\u00fb\n-\f-\16-\u00fe\13-\3-\3-\3.\3.\3.\3.\5.\u0106\n.\3.\3"+ + ".\3/\3/\3\60\6\60\u010d\n\60\r\60\16\60\u010e\3\60\3\60\3\61\3\61\3\61"+ + "\3\61\7\61\u0117\n\61\f\61\16\61\u011a\13\61\3\61\5\61\u011d\n\61\3\61"+ + "\3\61\3\61\3\61\3\61\7\61\u0124\n\61\f\61\16\61\u0127\13\61\3\61\3\61"+ + "\5\61\u012b\n\61\3\61\3\61\3\62\3\62\3\63\3\63\4\u00fc\u0125\2\64\3\3"+ + "\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31\16\33\17\35\20\37\21"+ + "!\22#\23%\24\'\25)\26+\27-\30/\31\61\32\63\33\65\34\67\359\36;\37= ?!"+ + "A\"C#E$G%I&K\'M(O)Q*S+U,W-Y.[/]\60_\61a\62c\2e\2\3\2\6\3\2$$\5\2\13\f"+ + "\16\17\"\"\4\2\f\f\17\17\4\2C\\c|\2\u013a\2\3\3\2\2\2\2\5\3\2\2\2\2\7"+ + "\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2"+ + "\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\2"+ + "\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2\2\2#\3\2\2\2\2%\3\2\2\2\2\'\3\2\2\2"+ + "\2)\3\2\2\2\2+\3\2\2\2\2-\3\2\2\2\2/\3\2\2\2\2\61\3\2\2\2\2\63\3\2\2\2"+ + "\2\65\3\2\2\2\2\67\3\2\2\2\29\3\2\2\2\2;\3\2\2\2\2=\3\2\2\2\2?\3\2\2\2"+ + "\2A\3\2\2\2\2C\3\2\2\2\2E\3\2\2\2\2G\3\2\2\2\2I\3\2\2\2\2K\3\2\2\2\2M"+ + "\3\2\2\2\2O\3\2\2\2\2Q\3\2\2\2\2S\3\2\2\2\2U\3\2\2\2\2W\3\2\2\2\2Y\3\2"+ + "\2\2\2[\3\2\2\2\2]\3\2\2\2\2_\3\2\2\2\2a\3\2\2\2\3g\3\2\2\2\5i\3\2\2\2"+ + "\7k\3\2\2\2\tm\3\2\2\2\13o\3\2\2\2\rt\3\2\2\2\17v\3\2\2\2\21x\3\2\2\2"+ + "\23|\3\2\2\2\25\u0081\3\2\2\2\27\u0086\3\2\2\2\31\u0089\3\2\2\2\33\u008b"+ + "\3\2\2\2\35\u008d\3\2\2\2\37\u0090\3\2\2\2!\u0095\3\2\2\2#\u009b\3\2\2"+ + "\2%\u00a2\3\2\2\2\'\u00a8\3\2\2\2)\u00aa\3\2\2\2+\u00ad\3\2\2\2-\u00b0"+ + "\3\2\2\2/\u00b3\3\2\2\2\61\u00b6\3\2\2\2\63\u00b9\3\2\2\2\65\u00bc\3\2"+ + "\2\2\67\u00bf\3\2\2\29\u00c2\3\2\2\2;\u00c5\3\2\2\2=\u00c7\3\2\2\2?\u00c9"+ + "\3\2\2\2A\u00cc\3\2\2\2C\u00cf\3\2\2\2E\u00d2\3\2\2\2G\u00d4\3\2\2\2I"+ + "\u00d6\3\2\2\2K\u00d8\3\2\2\2M\u00da\3\2\2\2O\u00dc\3\2\2\2Q\u00de\3\2"+ + "\2\2S\u00e3\3\2\2\2U\u00e9\3\2\2\2W\u00f2\3\2\2\2Y\u00f6\3\2\2\2[\u0101"+ + "\3\2\2\2]\u0109\3\2\2\2_\u010c\3\2\2\2a\u012a\3\2\2\2c\u012e\3\2\2\2e"+ + "\u0130\3\2\2\2gh\7.\2\2h\4\3\2\2\2ij\7=\2\2j\6\3\2\2\2kl\7]\2\2l\b\3\2"+ + "\2\2mn\7_\2\2n\n\3\2\2\2op\7x\2\2pq\7q\2\2qr\7k\2\2rs\7f\2\2s\f\3\2\2"+ + "\2tu\7*\2\2u\16\3\2\2\2vw\7+\2\2w\20\3\2\2\2xy\7k\2\2yz\7p\2\2z{\7v\2"+ + "\2{\22\3\2\2\2|}\7d\2\2}~\7q\2\2~\177\7q\2\2\177\u0080\7n\2\2\u0080\24"+ + "\3\2\2\2\u0081\u0082\7e\2\2\u0082\u0083\7j\2\2\u0083\u0084\7c\2\2\u0084"+ + "\u0085\7t\2\2\u0085\26\3\2\2\2\u0086\u0087\7]\2\2\u0087\u0088\7_\2\2\u0088"+ + "\30\3\2\2\2\u0089\u008a\7}\2\2\u008a\32\3\2\2\2\u008b\u008c\7\177\2\2"+ + "\u008c\34\3\2\2\2\u008d\u008e\7k\2\2\u008e\u008f\7h\2\2\u008f\36\3\2\2"+ + "\2\u0090\u0091\7g\2\2\u0091\u0092\7n\2\2\u0092\u0093\7u\2\2\u0093\u0094"+ + "\7g\2\2\u0094 \3\2\2\2\u0095\u0096\7y\2\2\u0096\u0097\7j\2\2\u0097\u0098"+ + "\7k\2\2\u0098\u0099\7n\2\2\u0099\u009a\7g\2\2\u009a\"\3\2\2\2\u009b\u009c"+ + "\7t\2\2\u009c\u009d\7g\2\2\u009d\u009e\7v\2\2\u009e\u009f\7w\2\2\u009f"+ + "\u00a0\7t\2\2\u00a0\u00a1\7p\2\2\u00a1$\3\2\2\2\u00a2\u00a3\7d\2\2\u00a3"+ + "\u00a4\7t\2\2\u00a4\u00a5\7g\2\2\u00a5\u00a6\7c\2\2\u00a6\u00a7\7m\2\2"+ + "\u00a7&\3\2\2\2\u00a8\u00a9\7?\2\2\u00a9(\3\2\2\2\u00aa\u00ab\7-\2\2\u00ab"+ + "\u00ac\7?\2\2\u00ac*\3\2\2\2\u00ad\u00ae\7/\2\2\u00ae\u00af\7?\2\2\u00af"+ + ",\3\2\2\2\u00b0\u00b1\7,\2\2\u00b1\u00b2\7?\2\2\u00b2.\3\2\2\2\u00b3\u00b4"+ + "\7\61\2\2\u00b4\u00b5\7?\2\2\u00b5\60\3\2\2\2\u00b6\u00b7\7-\2\2\u00b7"+ + "\u00b8\7-\2\2\u00b8\62\3\2\2\2\u00b9\u00ba\7/\2\2\u00ba\u00bb\7/\2\2\u00bb"+ + "\64\3\2\2\2\u00bc\u00bd\7~\2\2\u00bd\u00be\7~\2\2\u00be\66\3\2\2\2\u00bf"+ + "\u00c0\7(\2\2\u00c0\u00c1\7(\2\2\u00c18\3\2\2\2\u00c2\u00c3\7>\2\2\u00c3"+ + "\u00c4\7?\2\2\u00c4:\3\2\2\2\u00c5\u00c6\7>\2\2\u00c6<\3\2\2\2\u00c7\u00c8"+ + "\7@\2\2\u00c8>\3\2\2\2\u00c9\u00ca\7@\2\2\u00ca\u00cb\7?\2\2\u00cb@\3"+ + "\2\2\2\u00cc\u00cd\7?\2\2\u00cd\u00ce\7?\2\2\u00ceB\3\2\2\2\u00cf\u00d0"+ + "\7#\2\2\u00d0\u00d1\7?\2\2\u00d1D\3\2\2\2\u00d2\u00d3\7-\2\2\u00d3F\3"+ + "\2\2\2\u00d4\u00d5\7/\2\2\u00d5H\3\2\2\2\u00d6\u00d7\7,\2\2\u00d7J\3\2"+ + "\2\2\u00d8\u00d9\7\61\2\2\u00d9L\3\2\2\2\u00da\u00db\7\'\2\2\u00dbN\3"+ + "\2\2\2\u00dc\u00dd\7A\2\2\u00ddP\3\2\2\2\u00de\u00df\7v\2\2\u00df\u00e0"+ + "\7t\2\2\u00e0\u00e1\7w\2\2\u00e1\u00e2\7g\2\2\u00e2R\3\2\2\2\u00e3\u00e4"+ + "\7h\2\2\u00e4\u00e5\7c\2\2\u00e5\u00e6\7n\2\2\u00e6\u00e7\7u\2\2\u00e7"+ + "\u00e8\7g\2\2\u00e8T\3\2\2\2\u00e9\u00ee\5c\62\2\u00ea\u00ed\5c\62\2\u00eb"+ + "\u00ed\5e\63\2\u00ec\u00ea\3\2\2\2\u00ec\u00eb\3\2\2\2\u00ed\u00f0\3\2"+ + "\2\2\u00ee\u00ec\3\2\2\2\u00ee\u00ef\3\2\2\2\u00efV\3\2\2\2\u00f0\u00ee"+ + "\3\2\2\2\u00f1\u00f3\5e\63\2\u00f2\u00f1\3\2\2\2\u00f3\u00f4\3\2\2\2\u00f4"+ + "\u00f2\3\2\2\2\u00f4\u00f5\3\2\2\2\u00f5X\3\2\2\2\u00f6\u00fc\7$\2\2\u00f7"+ + "\u00f8\7^\2\2\u00f8\u00fb\7$\2\2\u00f9\u00fb\n\2\2\2\u00fa\u00f7\3\2\2"+ + "\2\u00fa\u00f9\3\2\2\2\u00fb\u00fe\3\2\2\2\u00fc\u00fd\3\2\2\2\u00fc\u00fa"+ + "\3\2\2\2\u00fd\u00ff\3\2\2\2\u00fe\u00fc\3\2\2\2\u00ff\u0100\7$\2\2\u0100"+ + "Z\3\2\2\2\u0101\u0105\7$\2\2\u0102\u0103\7^\2\2\u0103\u0106\7$\2\2\u0104"+ + "\u0106\n\2\2\2\u0105\u0102\3\2\2\2\u0105\u0104\3\2\2\2\u0106\u0107\3\2"+ + "\2\2\u0107\u0108\7$\2\2\u0108\\\3\2\2\2\u0109\u010a\7#\2\2\u010a^\3\2"+ + "\2\2\u010b\u010d\t\3\2\2\u010c\u010b\3\2\2\2\u010d\u010e\3\2\2\2\u010e"+ + "\u010c\3\2\2\2\u010e\u010f\3\2\2\2\u010f\u0110\3\2\2\2\u0110\u0111\b\60"+ + "\2\2\u0111`\3\2\2\2\u0112\u0113\7\61\2\2\u0113\u0114\7\61\2\2\u0114\u0118"+ + "\3\2\2\2\u0115\u0117\n\4\2\2\u0116\u0115\3\2\2\2\u0117\u011a\3\2\2\2\u0118"+ + "\u0116\3\2\2\2\u0118\u0119\3\2\2\2\u0119\u011c\3\2\2\2\u011a\u0118\3\2"+ + "\2\2\u011b\u011d\7\17\2\2\u011c\u011b\3\2\2\2\u011c\u011d\3\2\2\2\u011d"+ + "\u011e\3\2\2\2\u011e\u012b\7\f\2\2\u011f\u0120\7\61\2\2\u0120\u0121\7"+ + ",\2\2\u0121\u0125\3\2\2\2\u0122\u0124\13\2\2\2\u0123\u0122\3\2\2\2\u0124"+ + "\u0127\3\2\2\2\u0125\u0126\3\2\2\2\u0125\u0123\3\2\2\2\u0126\u0128\3\2"+ + "\2\2\u0127\u0125\3\2\2\2\u0128\u0129\7,\2\2\u0129\u012b\7\61\2\2\u012a"+ + "\u0112\3\2\2\2\u012a\u011f\3\2\2\2\u012b\u012c\3\2\2\2\u012c\u012d\b\61"+ + "\2\2\u012db\3\2\2\2\u012e\u012f\t\5\2\2\u012fd\3\2\2\2\u0130\u0131\4\62"+ + ";\2\u0131f\3\2\2\2\16\2\u00ec\u00ee\u00f4\u00fa\u00fc\u0105\u010e\u0118"+ + "\u011c\u0125\u012a\3\b\2\2"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +} \ No newline at end of file diff --git a/parser/CminusLexer.tokens b/parser/CminusLexer.tokens new file mode 100644 index 0000000..ae5a5c0 --- /dev/null +++ b/parser/CminusLexer.tokens @@ -0,0 +1,90 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +T__5=6 +T__6=7 +T__7=8 +T__8=9 +T__9=10 +T__10=11 +T__11=12 +T__12=13 +T__13=14 +T__14=15 +T__15=16 +T__16=17 +T__17=18 +T__18=19 +T__19=20 +T__20=21 +T__21=22 +T__22=23 +T__23=24 +T__24=25 +T__25=26 +T__26=27 +T__27=28 +T__28=29 +T__29=30 +T__30=31 +T__31=32 +T__32=33 +T__33=34 +T__34=35 +T__35=36 +T__36=37 +T__37=38 +T__38=39 +T__39=40 +T__40=41 +ID=42 +NUMCONST=43 +STRINGCONST=44 +CHARCONST=45 +BANG=46 +WS=47 +COMMENT=48 +','=1 +';'=2 +'['=3 +']'=4 +'void'=5 +'('=6 +')'=7 +'int'=8 +'bool'=9 +'char'=10 +'[]'=11 +'{'=12 +'}'=13 +'if'=14 +'else'=15 +'while'=16 +'return'=17 +'break'=18 +'='=19 +'+='=20 +'-='=21 +'*='=22 +'/='=23 +'++'=24 +'--'=25 +'||'=26 +'&&'=27 +'<='=28 +'<'=29 +'>'=30 +'>='=31 +'=='=32 +'!='=33 +'+'=34 +'-'=35 +'*'=36 +'/'=37 +'%'=38 +'?'=39 +'true'=40 +'false'=41 +'!'=46 diff --git a/parser/CminusListener.java b/parser/CminusListener.java new file mode 100644 index 0000000..bd141e3 --- /dev/null +++ b/parser/CminusListener.java @@ -0,0 +1,340 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.tree.ParseTreeListener; + +/** + * This interface defines a complete listener for a parse tree produced by + * {@link CminusParser}. + */ +public interface CminusListener extends ParseTreeListener { + /** + * Enter a parse tree produced by {@link CminusParser#program}. + * @param ctx the parse tree + */ + void enterProgram(CminusParser.ProgramContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#program}. + * @param ctx the parse tree + */ + void exitProgram(CminusParser.ProgramContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#declaration}. + * @param ctx the parse tree + */ + void enterDeclaration(CminusParser.DeclarationContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#declaration}. + * @param ctx the parse tree + */ + void exitDeclaration(CminusParser.DeclarationContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#varDeclaration}. + * @param ctx the parse tree + */ + void enterVarDeclaration(CminusParser.VarDeclarationContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#varDeclaration}. + * @param ctx the parse tree + */ + void exitVarDeclaration(CminusParser.VarDeclarationContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#varDeclId}. + * @param ctx the parse tree + */ + void enterVarDeclId(CminusParser.VarDeclIdContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#varDeclId}. + * @param ctx the parse tree + */ + void exitVarDeclId(CminusParser.VarDeclIdContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#funDeclaration}. + * @param ctx the parse tree + */ + void enterFunDeclaration(CminusParser.FunDeclarationContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#funDeclaration}. + * @param ctx the parse tree + */ + void exitFunDeclaration(CminusParser.FunDeclarationContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#typeSpecifier}. + * @param ctx the parse tree + */ + void enterTypeSpecifier(CminusParser.TypeSpecifierContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#typeSpecifier}. + * @param ctx the parse tree + */ + void exitTypeSpecifier(CminusParser.TypeSpecifierContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#param}. + * @param ctx the parse tree + */ + void enterParam(CminusParser.ParamContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#param}. + * @param ctx the parse tree + */ + void exitParam(CminusParser.ParamContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#paramId}. + * @param ctx the parse tree + */ + void enterParamId(CminusParser.ParamIdContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#paramId}. + * @param ctx the parse tree + */ + void exitParamId(CminusParser.ParamIdContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#statement}. + * @param ctx the parse tree + */ + void enterStatement(CminusParser.StatementContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#statement}. + * @param ctx the parse tree + */ + void exitStatement(CminusParser.StatementContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#compoundStmt}. + * @param ctx the parse tree + */ + void enterCompoundStmt(CminusParser.CompoundStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#compoundStmt}. + * @param ctx the parse tree + */ + void exitCompoundStmt(CminusParser.CompoundStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#expressionStmt}. + * @param ctx the parse tree + */ + void enterExpressionStmt(CminusParser.ExpressionStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#expressionStmt}. + * @param ctx the parse tree + */ + void exitExpressionStmt(CminusParser.ExpressionStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#ifStmt}. + * @param ctx the parse tree + */ + void enterIfStmt(CminusParser.IfStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#ifStmt}. + * @param ctx the parse tree + */ + void exitIfStmt(CminusParser.IfStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#whileStmt}. + * @param ctx the parse tree + */ + void enterWhileStmt(CminusParser.WhileStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#whileStmt}. + * @param ctx the parse tree + */ + void exitWhileStmt(CminusParser.WhileStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#returnStmt}. + * @param ctx the parse tree + */ + void enterReturnStmt(CminusParser.ReturnStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#returnStmt}. + * @param ctx the parse tree + */ + void exitReturnStmt(CminusParser.ReturnStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#breakStmt}. + * @param ctx the parse tree + */ + void enterBreakStmt(CminusParser.BreakStmtContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#breakStmt}. + * @param ctx the parse tree + */ + void exitBreakStmt(CminusParser.BreakStmtContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#expression}. + * @param ctx the parse tree + */ + void enterExpression(CminusParser.ExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#expression}. + * @param ctx the parse tree + */ + void exitExpression(CminusParser.ExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#simpleExpression}. + * @param ctx the parse tree + */ + void enterSimpleExpression(CminusParser.SimpleExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#simpleExpression}. + * @param ctx the parse tree + */ + void exitSimpleExpression(CminusParser.SimpleExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#orExpression}. + * @param ctx the parse tree + */ + void enterOrExpression(CminusParser.OrExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#orExpression}. + * @param ctx the parse tree + */ + void exitOrExpression(CminusParser.OrExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#andExpression}. + * @param ctx the parse tree + */ + void enterAndExpression(CminusParser.AndExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#andExpression}. + * @param ctx the parse tree + */ + void exitAndExpression(CminusParser.AndExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#unaryRelExpression}. + * @param ctx the parse tree + */ + void enterUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#unaryRelExpression}. + * @param ctx the parse tree + */ + void exitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#relExpression}. + * @param ctx the parse tree + */ + void enterRelExpression(CminusParser.RelExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#relExpression}. + * @param ctx the parse tree + */ + void exitRelExpression(CminusParser.RelExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#relop}. + * @param ctx the parse tree + */ + void enterRelop(CminusParser.RelopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#relop}. + * @param ctx the parse tree + */ + void exitRelop(CminusParser.RelopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#sumExpression}. + * @param ctx the parse tree + */ + void enterSumExpression(CminusParser.SumExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#sumExpression}. + * @param ctx the parse tree + */ + void exitSumExpression(CminusParser.SumExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#sumop}. + * @param ctx the parse tree + */ + void enterSumop(CminusParser.SumopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#sumop}. + * @param ctx the parse tree + */ + void exitSumop(CminusParser.SumopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#termExpression}. + * @param ctx the parse tree + */ + void enterTermExpression(CminusParser.TermExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#termExpression}. + * @param ctx the parse tree + */ + void exitTermExpression(CminusParser.TermExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#mulop}. + * @param ctx the parse tree + */ + void enterMulop(CminusParser.MulopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#mulop}. + * @param ctx the parse tree + */ + void exitMulop(CminusParser.MulopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#unaryExpression}. + * @param ctx the parse tree + */ + void enterUnaryExpression(CminusParser.UnaryExpressionContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#unaryExpression}. + * @param ctx the parse tree + */ + void exitUnaryExpression(CminusParser.UnaryExpressionContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#unaryop}. + * @param ctx the parse tree + */ + void enterUnaryop(CminusParser.UnaryopContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#unaryop}. + * @param ctx the parse tree + */ + void exitUnaryop(CminusParser.UnaryopContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#factor}. + * @param ctx the parse tree + */ + void enterFactor(CminusParser.FactorContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#factor}. + * @param ctx the parse tree + */ + void exitFactor(CminusParser.FactorContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#mutable}. + * @param ctx the parse tree + */ + void enterMutable(CminusParser.MutableContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#mutable}. + * @param ctx the parse tree + */ + void exitMutable(CminusParser.MutableContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#immutable}. + * @param ctx the parse tree + */ + void enterImmutable(CminusParser.ImmutableContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#immutable}. + * @param ctx the parse tree + */ + void exitImmutable(CminusParser.ImmutableContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#call}. + * @param ctx the parse tree + */ + void enterCall(CminusParser.CallContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#call}. + * @param ctx the parse tree + */ + void exitCall(CminusParser.CallContext ctx); + /** + * Enter a parse tree produced by {@link CminusParser#constant}. + * @param ctx the parse tree + */ + void enterConstant(CminusParser.ConstantContext ctx); + /** + * Exit a parse tree produced by {@link CminusParser#constant}. + * @param ctx the parse tree + */ + void exitConstant(CminusParser.ConstantContext ctx); +} \ No newline at end of file diff --git a/parser/CminusParser.java b/parser/CminusParser.java new file mode 100644 index 0000000..6749b21 --- /dev/null +++ b/parser/CminusParser.java @@ -0,0 +1,2454 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.misc.*; +import org.antlr.v4.runtime.tree.*; +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class CminusParser extends Parser { + static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9, + T__9=10, T__10=11, T__11=12, T__12=13, T__13=14, T__14=15, T__15=16, T__16=17, + T__17=18, T__18=19, T__19=20, T__20=21, T__21=22, T__22=23, T__23=24, + T__24=25, T__25=26, T__26=27, T__27=28, T__28=29, T__29=30, T__30=31, + T__31=32, T__32=33, T__33=34, T__34=35, T__35=36, T__36=37, T__37=38, + T__38=39, T__39=40, T__40=41, ID=42, NUMCONST=43, STRINGCONST=44, CHARCONST=45, + BANG=46, WS=47, COMMENT=48; + public static final int + RULE_program = 0, RULE_declaration = 1, RULE_varDeclaration = 2, RULE_varDeclId = 3, + RULE_funDeclaration = 4, RULE_typeSpecifier = 5, RULE_param = 6, RULE_paramId = 7, + RULE_statement = 8, RULE_compoundStmt = 9, RULE_expressionStmt = 10, RULE_ifStmt = 11, + RULE_whileStmt = 12, RULE_returnStmt = 13, RULE_breakStmt = 14, RULE_expression = 15, + RULE_simpleExpression = 16, RULE_orExpression = 17, RULE_andExpression = 18, + RULE_unaryRelExpression = 19, RULE_relExpression = 20, RULE_relop = 21, + RULE_sumExpression = 22, RULE_sumop = 23, RULE_termExpression = 24, RULE_mulop = 25, + RULE_unaryExpression = 26, RULE_unaryop = 27, RULE_factor = 28, RULE_mutable = 29, + RULE_immutable = 30, RULE_call = 31, RULE_constant = 32; + private static String[] makeRuleNames() { + return new String[] { + "program", "declaration", "varDeclaration", "varDeclId", "funDeclaration", + "typeSpecifier", "param", "paramId", "statement", "compoundStmt", "expressionStmt", + "ifStmt", "whileStmt", "returnStmt", "breakStmt", "expression", "simpleExpression", + "orExpression", "andExpression", "unaryRelExpression", "relExpression", + "relop", "sumExpression", "sumop", "termExpression", "mulop", "unaryExpression", + "unaryop", "factor", "mutable", "immutable", "call", "constant" + }; + } + public static final String[] ruleNames = makeRuleNames(); + + private static String[] makeLiteralNames() { + return new String[] { + null, "','", "';'", "'['", "']'", "'void'", "'('", "')'", "'int'", "'bool'", + "'char'", "'[]'", "'{'", "'}'", "'if'", "'else'", "'while'", "'return'", + "'break'", "'='", "'+='", "'-='", "'*='", "'/='", "'++'", "'--'", "'||'", + "'&&'", "'<='", "'<'", "'>'", "'>='", "'=='", "'!='", "'+'", "'-'", "'*'", + "'/'", "'%'", "'?'", "'true'", "'false'", null, null, null, null, "'!'" + }; + } + private static final String[] _LITERAL_NAMES = makeLiteralNames(); + private static String[] makeSymbolicNames() { + return new String[] { + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, + null, null, null, null, null, null, "ID", "NUMCONST", "STRINGCONST", + "CHARCONST", "BANG", "WS", "COMMENT" + }; + } + private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames(); + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + @Override + public String getGrammarFileName() { return "Cminus.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public ATN getATN() { return _ATN; } + + public CminusParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + public static class ProgramContext extends ParserRuleContext { + public List declaration() { + return getRuleContexts(DeclarationContext.class); + } + public DeclarationContext declaration(int i) { + return getRuleContext(DeclarationContext.class,i); + } + public ProgramContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_program; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterProgram(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitProgram(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitProgram(this); + else return visitor.visitChildren(this); + } + } + + public final ProgramContext program() throws RecognitionException { + ProgramContext _localctx = new ProgramContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_program); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(67); + _errHandler.sync(this); + _la = _input.LA(1); + do { + { + { + setState(66); + declaration(); + } + } + setState(69); + _errHandler.sync(this); + _la = _input.LA(1); + } while ( (((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__4) | (1L << T__7) | (1L << T__8) | (1L << T__9))) != 0) ); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class DeclarationContext extends ParserRuleContext { + public VarDeclarationContext varDeclaration() { + return getRuleContext(VarDeclarationContext.class,0); + } + public FunDeclarationContext funDeclaration() { + return getRuleContext(FunDeclarationContext.class,0); + } + public DeclarationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_declaration; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterDeclaration(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitDeclaration(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitDeclaration(this); + else return visitor.visitChildren(this); + } + } + + public final DeclarationContext declaration() throws RecognitionException { + DeclarationContext _localctx = new DeclarationContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_declaration); + try { + setState(73); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,1,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(71); + varDeclaration(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(72); + funDeclaration(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class VarDeclarationContext extends ParserRuleContext { + public TypeSpecifierContext typeSpecifier() { + return getRuleContext(TypeSpecifierContext.class,0); + } + public List varDeclId() { + return getRuleContexts(VarDeclIdContext.class); + } + public VarDeclIdContext varDeclId(int i) { + return getRuleContext(VarDeclIdContext.class,i); + } + public VarDeclarationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_varDeclaration; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterVarDeclaration(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitVarDeclaration(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitVarDeclaration(this); + else return visitor.visitChildren(this); + } + } + + public final VarDeclarationContext varDeclaration() throws RecognitionException { + VarDeclarationContext _localctx = new VarDeclarationContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_varDeclaration); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(75); + typeSpecifier(); + setState(76); + varDeclId(); + setState(81); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==T__0) { + { + { + setState(77); + match(T__0); + setState(78); + varDeclId(); + } + } + setState(83); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(84); + match(T__1); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class VarDeclIdContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public TerminalNode NUMCONST() { return getToken(CminusParser.NUMCONST, 0); } + public VarDeclIdContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_varDeclId; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterVarDeclId(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitVarDeclId(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitVarDeclId(this); + else return visitor.visitChildren(this); + } + } + + public final VarDeclIdContext varDeclId() throws RecognitionException { + VarDeclIdContext _localctx = new VarDeclIdContext(_ctx, getState()); + enterRule(_localctx, 6, RULE_varDeclId); + try { + setState(91); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(86); + match(ID); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(87); + match(ID); + setState(88); + match(T__2); + setState(89); + match(NUMCONST); + setState(90); + match(T__3); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class FunDeclarationContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public StatementContext statement() { + return getRuleContext(StatementContext.class,0); + } + public TypeSpecifierContext typeSpecifier() { + return getRuleContext(TypeSpecifierContext.class,0); + } + public List param() { + return getRuleContexts(ParamContext.class); + } + public ParamContext param(int i) { + return getRuleContext(ParamContext.class,i); + } + public FunDeclarationContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_funDeclaration; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterFunDeclaration(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitFunDeclaration(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitFunDeclaration(this); + else return visitor.visitChildren(this); + } + } + + public final FunDeclarationContext funDeclaration() throws RecognitionException { + FunDeclarationContext _localctx = new FunDeclarationContext(_ctx, getState()); + enterRule(_localctx, 8, RULE_funDeclaration); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(95); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__4: + { + setState(93); + match(T__4); + } + break; + case T__7: + case T__8: + case T__9: + { + setState(94); + typeSpecifier(); + } + break; + default: + throw new NoViableAltException(this); + } + setState(97); + match(ID); + setState(98); + match(T__5); + setState(100); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__7) | (1L << T__8) | (1L << T__9))) != 0)) { + { + setState(99); + param(); + } + } + + setState(106); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==T__0) { + { + { + setState(102); + match(T__0); + setState(103); + param(); + } + } + setState(108); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(109); + match(T__6); + setState(110); + statement(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class TypeSpecifierContext extends ParserRuleContext { + public TypeSpecifierContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_typeSpecifier; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterTypeSpecifier(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitTypeSpecifier(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitTypeSpecifier(this); + else return visitor.visitChildren(this); + } + } + + public final TypeSpecifierContext typeSpecifier() throws RecognitionException { + TypeSpecifierContext _localctx = new TypeSpecifierContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_typeSpecifier); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(112); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__7) | (1L << T__8) | (1L << T__9))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ParamContext extends ParserRuleContext { + public TypeSpecifierContext typeSpecifier() { + return getRuleContext(TypeSpecifierContext.class,0); + } + public ParamIdContext paramId() { + return getRuleContext(ParamIdContext.class,0); + } + public ParamContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_param; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterParam(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitParam(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitParam(this); + else return visitor.visitChildren(this); + } + } + + public final ParamContext param() throws RecognitionException { + ParamContext _localctx = new ParamContext(_ctx, getState()); + enterRule(_localctx, 12, RULE_param); + try { + enterOuterAlt(_localctx, 1); + { + setState(114); + typeSpecifier(); + setState(115); + paramId(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ParamIdContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public ParamIdContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_paramId; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterParamId(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitParamId(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitParamId(this); + else return visitor.visitChildren(this); + } + } + + public final ParamIdContext paramId() throws RecognitionException { + ParamIdContext _localctx = new ParamIdContext(_ctx, getState()); + enterRule(_localctx, 14, RULE_paramId); + try { + setState(120); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,7,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(117); + match(ID); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(118); + match(ID); + setState(119); + match(T__10); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class StatementContext extends ParserRuleContext { + public ExpressionStmtContext expressionStmt() { + return getRuleContext(ExpressionStmtContext.class,0); + } + public CompoundStmtContext compoundStmt() { + return getRuleContext(CompoundStmtContext.class,0); + } + public IfStmtContext ifStmt() { + return getRuleContext(IfStmtContext.class,0); + } + public WhileStmtContext whileStmt() { + return getRuleContext(WhileStmtContext.class,0); + } + public ReturnStmtContext returnStmt() { + return getRuleContext(ReturnStmtContext.class,0); + } + public BreakStmtContext breakStmt() { + return getRuleContext(BreakStmtContext.class,0); + } + public StatementContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_statement; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterStatement(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitStatement(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitStatement(this); + else return visitor.visitChildren(this); + } + } + + public final StatementContext statement() throws RecognitionException { + StatementContext _localctx = new StatementContext(_ctx, getState()); + enterRule(_localctx, 16, RULE_statement); + try { + setState(128); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__1: + case T__5: + case T__34: + case T__35: + case T__38: + case T__39: + case T__40: + case ID: + case NUMCONST: + case STRINGCONST: + case CHARCONST: + case BANG: + enterOuterAlt(_localctx, 1); + { + setState(122); + expressionStmt(); + } + break; + case T__11: + enterOuterAlt(_localctx, 2); + { + setState(123); + compoundStmt(); + } + break; + case T__13: + enterOuterAlt(_localctx, 3); + { + setState(124); + ifStmt(); + } + break; + case T__15: + enterOuterAlt(_localctx, 4); + { + setState(125); + whileStmt(); + } + break; + case T__16: + enterOuterAlt(_localctx, 5); + { + setState(126); + returnStmt(); + } + break; + case T__17: + enterOuterAlt(_localctx, 6); + { + setState(127); + breakStmt(); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class CompoundStmtContext extends ParserRuleContext { + public List varDeclaration() { + return getRuleContexts(VarDeclarationContext.class); + } + public VarDeclarationContext varDeclaration(int i) { + return getRuleContext(VarDeclarationContext.class,i); + } + public List statement() { + return getRuleContexts(StatementContext.class); + } + public StatementContext statement(int i) { + return getRuleContext(StatementContext.class,i); + } + public CompoundStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_compoundStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterCompoundStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitCompoundStmt(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitCompoundStmt(this); + else return visitor.visitChildren(this); + } + } + + public final CompoundStmtContext compoundStmt() throws RecognitionException { + CompoundStmtContext _localctx = new CompoundStmtContext(_ctx, getState()); + enterRule(_localctx, 18, RULE_compoundStmt); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(130); + match(T__11); + setState(134); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__7) | (1L << T__8) | (1L << T__9))) != 0)) { + { + { + setState(131); + varDeclaration(); + } + } + setState(136); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(140); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__1) | (1L << T__5) | (1L << T__11) | (1L << T__13) | (1L << T__15) | (1L << T__16) | (1L << T__17) | (1L << T__34) | (1L << T__35) | (1L << T__38) | (1L << T__39) | (1L << T__40) | (1L << ID) | (1L << NUMCONST) | (1L << STRINGCONST) | (1L << CHARCONST) | (1L << BANG))) != 0)) { + { + { + setState(137); + statement(); + } + } + setState(142); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(143); + match(T__12); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ExpressionStmtContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public ExpressionStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_expressionStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterExpressionStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitExpressionStmt(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitExpressionStmt(this); + else return visitor.visitChildren(this); + } + } + + public final ExpressionStmtContext expressionStmt() throws RecognitionException { + ExpressionStmtContext _localctx = new ExpressionStmtContext(_ctx, getState()); + enterRule(_localctx, 20, RULE_expressionStmt); + try { + setState(149); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__5: + case T__34: + case T__35: + case T__38: + case T__39: + case T__40: + case ID: + case NUMCONST: + case STRINGCONST: + case CHARCONST: + case BANG: + enterOuterAlt(_localctx, 1); + { + setState(145); + expression(); + setState(146); + match(T__1); + } + break; + case T__1: + enterOuterAlt(_localctx, 2); + { + setState(148); + match(T__1); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class IfStmtContext extends ParserRuleContext { + public SimpleExpressionContext simpleExpression() { + return getRuleContext(SimpleExpressionContext.class,0); + } + public List statement() { + return getRuleContexts(StatementContext.class); + } + public StatementContext statement(int i) { + return getRuleContext(StatementContext.class,i); + } + public IfStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_ifStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterIfStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitIfStmt(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitIfStmt(this); + else return visitor.visitChildren(this); + } + } + + public final IfStmtContext ifStmt() throws RecognitionException { + IfStmtContext _localctx = new IfStmtContext(_ctx, getState()); + enterRule(_localctx, 22, RULE_ifStmt); + try { + setState(165); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,12,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(151); + match(T__13); + setState(152); + match(T__5); + setState(153); + simpleExpression(); + setState(154); + match(T__6); + setState(155); + statement(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(157); + match(T__13); + setState(158); + match(T__5); + setState(159); + simpleExpression(); + setState(160); + match(T__6); + setState(161); + statement(); + setState(162); + match(T__14); + setState(163); + statement(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class WhileStmtContext extends ParserRuleContext { + public SimpleExpressionContext simpleExpression() { + return getRuleContext(SimpleExpressionContext.class,0); + } + public StatementContext statement() { + return getRuleContext(StatementContext.class,0); + } + public WhileStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_whileStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterWhileStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitWhileStmt(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitWhileStmt(this); + else return visitor.visitChildren(this); + } + } + + public final WhileStmtContext whileStmt() throws RecognitionException { + WhileStmtContext _localctx = new WhileStmtContext(_ctx, getState()); + enterRule(_localctx, 24, RULE_whileStmt); + try { + enterOuterAlt(_localctx, 1); + { + setState(167); + match(T__15); + setState(168); + match(T__5); + setState(169); + simpleExpression(); + setState(170); + match(T__6); + setState(171); + statement(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ReturnStmtContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public ReturnStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_returnStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterReturnStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitReturnStmt(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitReturnStmt(this); + else return visitor.visitChildren(this); + } + } + + public final ReturnStmtContext returnStmt() throws RecognitionException { + ReturnStmtContext _localctx = new ReturnStmtContext(_ctx, getState()); + enterRule(_localctx, 26, RULE_returnStmt); + try { + setState(179); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,13,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(173); + match(T__16); + setState(174); + match(T__1); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(175); + match(T__16); + setState(176); + expression(); + setState(177); + match(T__1); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class BreakStmtContext extends ParserRuleContext { + public BreakStmtContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_breakStmt; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterBreakStmt(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitBreakStmt(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitBreakStmt(this); + else return visitor.visitChildren(this); + } + } + + public final BreakStmtContext breakStmt() throws RecognitionException { + BreakStmtContext _localctx = new BreakStmtContext(_ctx, getState()); + enterRule(_localctx, 28, RULE_breakStmt); + try { + enterOuterAlt(_localctx, 1); + { + setState(181); + match(T__17); + setState(182); + match(T__1); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ExpressionContext extends ParserRuleContext { + public MutableContext mutable() { + return getRuleContext(MutableContext.class,0); + } + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public SimpleExpressionContext simpleExpression() { + return getRuleContext(SimpleExpressionContext.class,0); + } + public ExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_expression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitExpression(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitExpression(this); + else return visitor.visitChildren(this); + } + } + + public final ExpressionContext expression() throws RecognitionException { + ExpressionContext _localctx = new ExpressionContext(_ctx, getState()); + enterRule(_localctx, 30, RULE_expression); + try { + setState(211); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,14,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(184); + mutable(); + setState(185); + match(T__18); + setState(186); + expression(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(188); + mutable(); + setState(189); + match(T__19); + setState(190); + expression(); + } + break; + case 3: + enterOuterAlt(_localctx, 3); + { + setState(192); + mutable(); + setState(193); + match(T__20); + setState(194); + expression(); + } + break; + case 4: + enterOuterAlt(_localctx, 4); + { + setState(196); + mutable(); + setState(197); + match(T__21); + setState(198); + expression(); + } + break; + case 5: + enterOuterAlt(_localctx, 5); + { + setState(200); + mutable(); + setState(201); + match(T__22); + setState(202); + expression(); + } + break; + case 6: + enterOuterAlt(_localctx, 6); + { + setState(204); + mutable(); + setState(205); + match(T__23); + } + break; + case 7: + enterOuterAlt(_localctx, 7); + { + setState(207); + mutable(); + setState(208); + match(T__24); + } + break; + case 8: + enterOuterAlt(_localctx, 8); + { + setState(210); + simpleExpression(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SimpleExpressionContext extends ParserRuleContext { + public OrExpressionContext orExpression() { + return getRuleContext(OrExpressionContext.class,0); + } + public SimpleExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_simpleExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterSimpleExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitSimpleExpression(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitSimpleExpression(this); + else return visitor.visitChildren(this); + } + } + + public final SimpleExpressionContext simpleExpression() throws RecognitionException { + SimpleExpressionContext _localctx = new SimpleExpressionContext(_ctx, getState()); + enterRule(_localctx, 32, RULE_simpleExpression); + try { + enterOuterAlt(_localctx, 1); + { + setState(213); + orExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class OrExpressionContext extends ParserRuleContext { + public List andExpression() { + return getRuleContexts(AndExpressionContext.class); + } + public AndExpressionContext andExpression(int i) { + return getRuleContext(AndExpressionContext.class,i); + } + public OrExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_orExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterOrExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitOrExpression(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitOrExpression(this); + else return visitor.visitChildren(this); + } + } + + public final OrExpressionContext orExpression() throws RecognitionException { + OrExpressionContext _localctx = new OrExpressionContext(_ctx, getState()); + enterRule(_localctx, 34, RULE_orExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(220); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,15,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(215); + andExpression(); + setState(216); + match(T__25); + } + } + } + setState(222); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,15,_ctx); + } + setState(223); + andExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class AndExpressionContext extends ParserRuleContext { + public List unaryRelExpression() { + return getRuleContexts(UnaryRelExpressionContext.class); + } + public UnaryRelExpressionContext unaryRelExpression(int i) { + return getRuleContext(UnaryRelExpressionContext.class,i); + } + public AndExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_andExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterAndExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitAndExpression(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitAndExpression(this); + else return visitor.visitChildren(this); + } + } + + public final AndExpressionContext andExpression() throws RecognitionException { + AndExpressionContext _localctx = new AndExpressionContext(_ctx, getState()); + enterRule(_localctx, 36, RULE_andExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(230); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,16,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(225); + unaryRelExpression(); + setState(226); + match(T__26); + } + } + } + setState(232); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,16,_ctx); + } + setState(233); + unaryRelExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class UnaryRelExpressionContext extends ParserRuleContext { + public RelExpressionContext relExpression() { + return getRuleContext(RelExpressionContext.class,0); + } + public List BANG() { return getTokens(CminusParser.BANG); } + public TerminalNode BANG(int i) { + return getToken(CminusParser.BANG, i); + } + public UnaryRelExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_unaryRelExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterUnaryRelExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitUnaryRelExpression(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitUnaryRelExpression(this); + else return visitor.visitChildren(this); + } + } + + public final UnaryRelExpressionContext unaryRelExpression() throws RecognitionException { + UnaryRelExpressionContext _localctx = new UnaryRelExpressionContext(_ctx, getState()); + enterRule(_localctx, 38, RULE_unaryRelExpression); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(238); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==BANG) { + { + { + setState(235); + match(BANG); + } + } + setState(240); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(241); + relExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class RelExpressionContext extends ParserRuleContext { + public List sumExpression() { + return getRuleContexts(SumExpressionContext.class); + } + public SumExpressionContext sumExpression(int i) { + return getRuleContext(SumExpressionContext.class,i); + } + public List relop() { + return getRuleContexts(RelopContext.class); + } + public RelopContext relop(int i) { + return getRuleContext(RelopContext.class,i); + } + public RelExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_relExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterRelExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitRelExpression(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitRelExpression(this); + else return visitor.visitChildren(this); + } + } + + public final RelExpressionContext relExpression() throws RecognitionException { + RelExpressionContext _localctx = new RelExpressionContext(_ctx, getState()); + enterRule(_localctx, 40, RULE_relExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(248); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,18,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(243); + sumExpression(); + setState(244); + relop(); + } + } + } + setState(250); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,18,_ctx); + } + setState(251); + sumExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class RelopContext extends ParserRuleContext { + public RelopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_relop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterRelop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitRelop(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitRelop(this); + else return visitor.visitChildren(this); + } + } + + public final RelopContext relop() throws RecognitionException { + RelopContext _localctx = new RelopContext(_ctx, getState()); + enterRule(_localctx, 42, RULE_relop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(253); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__27) | (1L << T__28) | (1L << T__29) | (1L << T__30) | (1L << T__31) | (1L << T__32))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SumExpressionContext extends ParserRuleContext { + public List termExpression() { + return getRuleContexts(TermExpressionContext.class); + } + public TermExpressionContext termExpression(int i) { + return getRuleContext(TermExpressionContext.class,i); + } + public List sumop() { + return getRuleContexts(SumopContext.class); + } + public SumopContext sumop(int i) { + return getRuleContext(SumopContext.class,i); + } + public SumExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_sumExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterSumExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitSumExpression(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitSumExpression(this); + else return visitor.visitChildren(this); + } + } + + public final SumExpressionContext sumExpression() throws RecognitionException { + SumExpressionContext _localctx = new SumExpressionContext(_ctx, getState()); + enterRule(_localctx, 44, RULE_sumExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(260); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,19,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(255); + termExpression(); + setState(256); + sumop(); + } + } + } + setState(262); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,19,_ctx); + } + setState(263); + termExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SumopContext extends ParserRuleContext { + public SumopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_sumop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterSumop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitSumop(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitSumop(this); + else return visitor.visitChildren(this); + } + } + + public final SumopContext sumop() throws RecognitionException { + SumopContext _localctx = new SumopContext(_ctx, getState()); + enterRule(_localctx, 46, RULE_sumop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(265); + _la = _input.LA(1); + if ( !(_la==T__33 || _la==T__34) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class TermExpressionContext extends ParserRuleContext { + public List unaryExpression() { + return getRuleContexts(UnaryExpressionContext.class); + } + public UnaryExpressionContext unaryExpression(int i) { + return getRuleContext(UnaryExpressionContext.class,i); + } + public List mulop() { + return getRuleContexts(MulopContext.class); + } + public MulopContext mulop(int i) { + return getRuleContext(MulopContext.class,i); + } + public TermExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_termExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterTermExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitTermExpression(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitTermExpression(this); + else return visitor.visitChildren(this); + } + } + + public final TermExpressionContext termExpression() throws RecognitionException { + TermExpressionContext _localctx = new TermExpressionContext(_ctx, getState()); + enterRule(_localctx, 48, RULE_termExpression); + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(272); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,20,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(267); + unaryExpression(); + setState(268); + mulop(); + } + } + } + setState(274); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,20,_ctx); + } + setState(275); + unaryExpression(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class MulopContext extends ParserRuleContext { + public MulopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_mulop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterMulop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitMulop(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitMulop(this); + else return visitor.visitChildren(this); + } + } + + public final MulopContext mulop() throws RecognitionException { + MulopContext _localctx = new MulopContext(_ctx, getState()); + enterRule(_localctx, 50, RULE_mulop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(277); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__35) | (1L << T__36) | (1L << T__37))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class UnaryExpressionContext extends ParserRuleContext { + public FactorContext factor() { + return getRuleContext(FactorContext.class,0); + } + public List unaryop() { + return getRuleContexts(UnaryopContext.class); + } + public UnaryopContext unaryop(int i) { + return getRuleContext(UnaryopContext.class,i); + } + public UnaryExpressionContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_unaryExpression; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterUnaryExpression(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitUnaryExpression(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitUnaryExpression(this); + else return visitor.visitChildren(this); + } + } + + public final UnaryExpressionContext unaryExpression() throws RecognitionException { + UnaryExpressionContext _localctx = new UnaryExpressionContext(_ctx, getState()); + enterRule(_localctx, 52, RULE_unaryExpression); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(282); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__34) | (1L << T__35) | (1L << T__38))) != 0)) { + { + { + setState(279); + unaryop(); + } + } + setState(284); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(285); + factor(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class UnaryopContext extends ParserRuleContext { + public UnaryopContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_unaryop; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterUnaryop(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitUnaryop(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitUnaryop(this); + else return visitor.visitChildren(this); + } + } + + public final UnaryopContext unaryop() throws RecognitionException { + UnaryopContext _localctx = new UnaryopContext(_ctx, getState()); + enterRule(_localctx, 54, RULE_unaryop); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(287); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__34) | (1L << T__35) | (1L << T__38))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class FactorContext extends ParserRuleContext { + public ImmutableContext immutable() { + return getRuleContext(ImmutableContext.class,0); + } + public MutableContext mutable() { + return getRuleContext(MutableContext.class,0); + } + public FactorContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_factor; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterFactor(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitFactor(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitFactor(this); + else return visitor.visitChildren(this); + } + } + + public final FactorContext factor() throws RecognitionException { + FactorContext _localctx = new FactorContext(_ctx, getState()); + enterRule(_localctx, 56, RULE_factor); + try { + setState(291); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,22,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(289); + immutable(); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(290); + mutable(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class MutableContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public MutableContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_mutable; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterMutable(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitMutable(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitMutable(this); + else return visitor.visitChildren(this); + } + } + + public final MutableContext mutable() throws RecognitionException { + MutableContext _localctx = new MutableContext(_ctx, getState()); + enterRule(_localctx, 58, RULE_mutable); + try { + setState(299); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,23,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(293); + match(ID); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(294); + match(ID); + setState(295); + match(T__2); + setState(296); + expression(); + setState(297); + match(T__3); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ImmutableContext extends ParserRuleContext { + public ExpressionContext expression() { + return getRuleContext(ExpressionContext.class,0); + } + public CallContext call() { + return getRuleContext(CallContext.class,0); + } + public ConstantContext constant() { + return getRuleContext(ConstantContext.class,0); + } + public ImmutableContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_immutable; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterImmutable(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitImmutable(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitImmutable(this); + else return visitor.visitChildren(this); + } + } + + public final ImmutableContext immutable() throws RecognitionException { + ImmutableContext _localctx = new ImmutableContext(_ctx, getState()); + enterRule(_localctx, 60, RULE_immutable); + try { + setState(307); + _errHandler.sync(this); + switch (_input.LA(1)) { + case T__5: + enterOuterAlt(_localctx, 1); + { + setState(301); + match(T__5); + setState(302); + expression(); + setState(303); + match(T__6); + } + break; + case ID: + enterOuterAlt(_localctx, 2); + { + setState(305); + call(); + } + break; + case T__39: + case T__40: + case NUMCONST: + case STRINGCONST: + case CHARCONST: + enterOuterAlt(_localctx, 3); + { + setState(306); + constant(); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class CallContext extends ParserRuleContext { + public TerminalNode ID() { return getToken(CminusParser.ID, 0); } + public List expression() { + return getRuleContexts(ExpressionContext.class); + } + public ExpressionContext expression(int i) { + return getRuleContext(ExpressionContext.class,i); + } + public CallContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_call; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterCall(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitCall(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitCall(this); + else return visitor.visitChildren(this); + } + } + + public final CallContext call() throws RecognitionException { + CallContext _localctx = new CallContext(_ctx, getState()); + enterRule(_localctx, 62, RULE_call); + int _la; + try { + int _alt; + enterOuterAlt(_localctx, 1); + { + setState(309); + match(ID); + setState(310); + match(T__5); + setState(316); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,25,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(311); + expression(); + setState(312); + match(T__0); + } + } + } + setState(318); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,25,_ctx); + } + setState(320); + _errHandler.sync(this); + _la = _input.LA(1); + if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__5) | (1L << T__34) | (1L << T__35) | (1L << T__38) | (1L << T__39) | (1L << T__40) | (1L << ID) | (1L << NUMCONST) | (1L << STRINGCONST) | (1L << CHARCONST) | (1L << BANG))) != 0)) { + { + setState(319); + expression(); + } + } + + setState(322); + match(T__6); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ConstantContext extends ParserRuleContext { + public TerminalNode NUMCONST() { return getToken(CminusParser.NUMCONST, 0); } + public TerminalNode CHARCONST() { return getToken(CminusParser.CHARCONST, 0); } + public TerminalNode STRINGCONST() { return getToken(CminusParser.STRINGCONST, 0); } + public ConstantContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_constant; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).enterConstant(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof CminusListener ) ((CminusListener)listener).exitConstant(this); + } + @Override + public T accept(ParseTreeVisitor visitor) { + if ( visitor instanceof CminusVisitor ) return ((CminusVisitor)visitor).visitConstant(this); + else return visitor.visitChildren(this); + } + } + + public final ConstantContext constant() throws RecognitionException { + ConstantContext _localctx = new ConstantContext(_ctx, getState()); + enterRule(_localctx, 64, RULE_constant); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(324); + _la = _input.LA(1); + if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << T__39) | (1L << T__40) | (1L << NUMCONST) | (1L << STRINGCONST) | (1L << CHARCONST))) != 0)) ) { + _errHandler.recoverInline(this); + } + else { + if ( _input.LA(1)==Token.EOF ) matchedEOF = true; + _errHandler.reportMatch(this); + consume(); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\62\u0149\4\2\t\2"+ + "\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13"+ + "\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22"+ + "\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31"+ + "\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t \4!"+ + "\t!\4\"\t\"\3\2\6\2F\n\2\r\2\16\2G\3\3\3\3\5\3L\n\3\3\4\3\4\3\4\3\4\7"+ + "\4R\n\4\f\4\16\4U\13\4\3\4\3\4\3\5\3\5\3\5\3\5\3\5\5\5^\n\5\3\6\3\6\5"+ + "\6b\n\6\3\6\3\6\3\6\5\6g\n\6\3\6\3\6\7\6k\n\6\f\6\16\6n\13\6\3\6\3\6\3"+ + "\6\3\7\3\7\3\b\3\b\3\b\3\t\3\t\3\t\5\t{\n\t\3\n\3\n\3\n\3\n\3\n\3\n\5"+ + "\n\u0083\n\n\3\13\3\13\7\13\u0087\n\13\f\13\16\13\u008a\13\13\3\13\7\13"+ + "\u008d\n\13\f\13\16\13\u0090\13\13\3\13\3\13\3\f\3\f\3\f\3\f\5\f\u0098"+ + "\n\f\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\3\r\5\r\u00a8"+ + "\n\r\3\16\3\16\3\16\3\16\3\16\3\16\3\17\3\17\3\17\3\17\3\17\3\17\5\17"+ + "\u00b6\n\17\3\20\3\20\3\20\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21"+ + "\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21\3\21"+ + "\3\21\3\21\3\21\3\21\5\21\u00d6\n\21\3\22\3\22\3\23\3\23\3\23\7\23\u00dd"+ + "\n\23\f\23\16\23\u00e0\13\23\3\23\3\23\3\24\3\24\3\24\7\24\u00e7\n\24"+ + "\f\24\16\24\u00ea\13\24\3\24\3\24\3\25\7\25\u00ef\n\25\f\25\16\25\u00f2"+ + "\13\25\3\25\3\25\3\26\3\26\3\26\7\26\u00f9\n\26\f\26\16\26\u00fc\13\26"+ + "\3\26\3\26\3\27\3\27\3\30\3\30\3\30\7\30\u0105\n\30\f\30\16\30\u0108\13"+ + "\30\3\30\3\30\3\31\3\31\3\32\3\32\3\32\7\32\u0111\n\32\f\32\16\32\u0114"+ + "\13\32\3\32\3\32\3\33\3\33\3\34\7\34\u011b\n\34\f\34\16\34\u011e\13\34"+ + "\3\34\3\34\3\35\3\35\3\36\3\36\5\36\u0126\n\36\3\37\3\37\3\37\3\37\3\37"+ + "\3\37\5\37\u012e\n\37\3 \3 \3 \3 \3 \3 \5 \u0136\n \3!\3!\3!\3!\3!\7!"+ + "\u013d\n!\f!\16!\u0140\13!\3!\5!\u0143\n!\3!\3!\3\"\3\"\3\"\2\2#\2\4\6"+ + "\b\n\f\16\20\22\24\26\30\32\34\36 \"$&(*,.\60\62\64\668:<>@B\2\b\3\2\n"+ + "\f\3\2\36#\3\2$%\3\2&(\4\2%&))\4\2*+-/\2\u014d\2E\3\2\2\2\4K\3\2\2\2\6"+ + "M\3\2\2\2\b]\3\2\2\2\na\3\2\2\2\fr\3\2\2\2\16t\3\2\2\2\20z\3\2\2\2\22"+ + "\u0082\3\2\2\2\24\u0084\3\2\2\2\26\u0097\3\2\2\2\30\u00a7\3\2\2\2\32\u00a9"+ + "\3\2\2\2\34\u00b5\3\2\2\2\36\u00b7\3\2\2\2 \u00d5\3\2\2\2\"\u00d7\3\2"+ + "\2\2$\u00de\3\2\2\2&\u00e8\3\2\2\2(\u00f0\3\2\2\2*\u00fa\3\2\2\2,\u00ff"+ + "\3\2\2\2.\u0106\3\2\2\2\60\u010b\3\2\2\2\62\u0112\3\2\2\2\64\u0117\3\2"+ + "\2\2\66\u011c\3\2\2\28\u0121\3\2\2\2:\u0125\3\2\2\2<\u012d\3\2\2\2>\u0135"+ + "\3\2\2\2@\u0137\3\2\2\2B\u0146\3\2\2\2DF\5\4\3\2ED\3\2\2\2FG\3\2\2\2G"+ + "E\3\2\2\2GH\3\2\2\2H\3\3\2\2\2IL\5\6\4\2JL\5\n\6\2KI\3\2\2\2KJ\3\2\2\2"+ + "L\5\3\2\2\2MN\5\f\7\2NS\5\b\5\2OP\7\3\2\2PR\5\b\5\2QO\3\2\2\2RU\3\2\2"+ + "\2SQ\3\2\2\2ST\3\2\2\2TV\3\2\2\2US\3\2\2\2VW\7\4\2\2W\7\3\2\2\2X^\7,\2"+ + "\2YZ\7,\2\2Z[\7\5\2\2[\\\7-\2\2\\^\7\6\2\2]X\3\2\2\2]Y\3\2\2\2^\t\3\2"+ + "\2\2_b\7\7\2\2`b\5\f\7\2a_\3\2\2\2a`\3\2\2\2bc\3\2\2\2cd\7,\2\2df\7\b"+ + "\2\2eg\5\16\b\2fe\3\2\2\2fg\3\2\2\2gl\3\2\2\2hi\7\3\2\2ik\5\16\b\2jh\3"+ + "\2\2\2kn\3\2\2\2lj\3\2\2\2lm\3\2\2\2mo\3\2\2\2nl\3\2\2\2op\7\t\2\2pq\5"+ + "\22\n\2q\13\3\2\2\2rs\t\2\2\2s\r\3\2\2\2tu\5\f\7\2uv\5\20\t\2v\17\3\2"+ + "\2\2w{\7,\2\2xy\7,\2\2y{\7\r\2\2zw\3\2\2\2zx\3\2\2\2{\21\3\2\2\2|\u0083"+ + "\5\26\f\2}\u0083\5\24\13\2~\u0083\5\30\r\2\177\u0083\5\32\16\2\u0080\u0083"+ + "\5\34\17\2\u0081\u0083\5\36\20\2\u0082|\3\2\2\2\u0082}\3\2\2\2\u0082~"+ + "\3\2\2\2\u0082\177\3\2\2\2\u0082\u0080\3\2\2\2\u0082\u0081\3\2\2\2\u0083"+ + "\23\3\2\2\2\u0084\u0088\7\16\2\2\u0085\u0087\5\6\4\2\u0086\u0085\3\2\2"+ + "\2\u0087\u008a\3\2\2\2\u0088\u0086\3\2\2\2\u0088\u0089\3\2\2\2\u0089\u008e"+ + "\3\2\2\2\u008a\u0088\3\2\2\2\u008b\u008d\5\22\n\2\u008c\u008b\3\2\2\2"+ + "\u008d\u0090\3\2\2\2\u008e\u008c\3\2\2\2\u008e\u008f\3\2\2\2\u008f\u0091"+ + "\3\2\2\2\u0090\u008e\3\2\2\2\u0091\u0092\7\17\2\2\u0092\25\3\2\2\2\u0093"+ + "\u0094\5 \21\2\u0094\u0095\7\4\2\2\u0095\u0098\3\2\2\2\u0096\u0098\7\4"+ + "\2\2\u0097\u0093\3\2\2\2\u0097\u0096\3\2\2\2\u0098\27\3\2\2\2\u0099\u009a"+ + "\7\20\2\2\u009a\u009b\7\b\2\2\u009b\u009c\5\"\22\2\u009c\u009d\7\t\2\2"+ + "\u009d\u009e\5\22\n\2\u009e\u00a8\3\2\2\2\u009f\u00a0\7\20\2\2\u00a0\u00a1"+ + "\7\b\2\2\u00a1\u00a2\5\"\22\2\u00a2\u00a3\7\t\2\2\u00a3\u00a4\5\22\n\2"+ + "\u00a4\u00a5\7\21\2\2\u00a5\u00a6\5\22\n\2\u00a6\u00a8\3\2\2\2\u00a7\u0099"+ + "\3\2\2\2\u00a7\u009f\3\2\2\2\u00a8\31\3\2\2\2\u00a9\u00aa\7\22\2\2\u00aa"+ + "\u00ab\7\b\2\2\u00ab\u00ac\5\"\22\2\u00ac\u00ad\7\t\2\2\u00ad\u00ae\5"+ + "\22\n\2\u00ae\33\3\2\2\2\u00af\u00b0\7\23\2\2\u00b0\u00b6\7\4\2\2\u00b1"+ + "\u00b2\7\23\2\2\u00b2\u00b3\5 \21\2\u00b3\u00b4\7\4\2\2\u00b4\u00b6\3"+ + "\2\2\2\u00b5\u00af\3\2\2\2\u00b5\u00b1\3\2\2\2\u00b6\35\3\2\2\2\u00b7"+ + "\u00b8\7\24\2\2\u00b8\u00b9\7\4\2\2\u00b9\37\3\2\2\2\u00ba\u00bb\5<\37"+ + "\2\u00bb\u00bc\7\25\2\2\u00bc\u00bd\5 \21\2\u00bd\u00d6\3\2\2\2\u00be"+ + "\u00bf\5<\37\2\u00bf\u00c0\7\26\2\2\u00c0\u00c1\5 \21\2\u00c1\u00d6\3"+ + "\2\2\2\u00c2\u00c3\5<\37\2\u00c3\u00c4\7\27\2\2\u00c4\u00c5\5 \21\2\u00c5"+ + "\u00d6\3\2\2\2\u00c6\u00c7\5<\37\2\u00c7\u00c8\7\30\2\2\u00c8\u00c9\5"+ + " \21\2\u00c9\u00d6\3\2\2\2\u00ca\u00cb\5<\37\2\u00cb\u00cc\7\31\2\2\u00cc"+ + "\u00cd\5 \21\2\u00cd\u00d6\3\2\2\2\u00ce\u00cf\5<\37\2\u00cf\u00d0\7\32"+ + "\2\2\u00d0\u00d6\3\2\2\2\u00d1\u00d2\5<\37\2\u00d2\u00d3\7\33\2\2\u00d3"+ + "\u00d6\3\2\2\2\u00d4\u00d6\5\"\22\2\u00d5\u00ba\3\2\2\2\u00d5\u00be\3"+ + "\2\2\2\u00d5\u00c2\3\2\2\2\u00d5\u00c6\3\2\2\2\u00d5\u00ca\3\2\2\2\u00d5"+ + "\u00ce\3\2\2\2\u00d5\u00d1\3\2\2\2\u00d5\u00d4\3\2\2\2\u00d6!\3\2\2\2"+ + "\u00d7\u00d8\5$\23\2\u00d8#\3\2\2\2\u00d9\u00da\5&\24\2\u00da\u00db\7"+ + "\34\2\2\u00db\u00dd\3\2\2\2\u00dc\u00d9\3\2\2\2\u00dd\u00e0\3\2\2\2\u00de"+ + "\u00dc\3\2\2\2\u00de\u00df\3\2\2\2\u00df\u00e1\3\2\2\2\u00e0\u00de\3\2"+ + "\2\2\u00e1\u00e2\5&\24\2\u00e2%\3\2\2\2\u00e3\u00e4\5(\25\2\u00e4\u00e5"+ + "\7\35\2\2\u00e5\u00e7\3\2\2\2\u00e6\u00e3\3\2\2\2\u00e7\u00ea\3\2\2\2"+ + "\u00e8\u00e6\3\2\2\2\u00e8\u00e9\3\2\2\2\u00e9\u00eb\3\2\2\2\u00ea\u00e8"+ + "\3\2\2\2\u00eb\u00ec\5(\25\2\u00ec\'\3\2\2\2\u00ed\u00ef\7\60\2\2\u00ee"+ + "\u00ed\3\2\2\2\u00ef\u00f2\3\2\2\2\u00f0\u00ee\3\2\2\2\u00f0\u00f1\3\2"+ + "\2\2\u00f1\u00f3\3\2\2\2\u00f2\u00f0\3\2\2\2\u00f3\u00f4\5*\26\2\u00f4"+ + ")\3\2\2\2\u00f5\u00f6\5.\30\2\u00f6\u00f7\5,\27\2\u00f7\u00f9\3\2\2\2"+ + "\u00f8\u00f5\3\2\2\2\u00f9\u00fc\3\2\2\2\u00fa\u00f8\3\2\2\2\u00fa\u00fb"+ + "\3\2\2\2\u00fb\u00fd\3\2\2\2\u00fc\u00fa\3\2\2\2\u00fd\u00fe\5.\30\2\u00fe"+ + "+\3\2\2\2\u00ff\u0100\t\3\2\2\u0100-\3\2\2\2\u0101\u0102\5\62\32\2\u0102"+ + "\u0103\5\60\31\2\u0103\u0105\3\2\2\2\u0104\u0101\3\2\2\2\u0105\u0108\3"+ + "\2\2\2\u0106\u0104\3\2\2\2\u0106\u0107\3\2\2\2\u0107\u0109\3\2\2\2\u0108"+ + "\u0106\3\2\2\2\u0109\u010a\5\62\32\2\u010a/\3\2\2\2\u010b\u010c\t\4\2"+ + "\2\u010c\61\3\2\2\2\u010d\u010e\5\66\34\2\u010e\u010f\5\64\33\2\u010f"+ + "\u0111\3\2\2\2\u0110\u010d\3\2\2\2\u0111\u0114\3\2\2\2\u0112\u0110\3\2"+ + "\2\2\u0112\u0113\3\2\2\2\u0113\u0115\3\2\2\2\u0114\u0112\3\2\2\2\u0115"+ + "\u0116\5\66\34\2\u0116\63\3\2\2\2\u0117\u0118\t\5\2\2\u0118\65\3\2\2\2"+ + "\u0119\u011b\58\35\2\u011a\u0119\3\2\2\2\u011b\u011e\3\2\2\2\u011c\u011a"+ + "\3\2\2\2\u011c\u011d\3\2\2\2\u011d\u011f\3\2\2\2\u011e\u011c\3\2\2\2\u011f"+ + "\u0120\5:\36\2\u0120\67\3\2\2\2\u0121\u0122\t\6\2\2\u01229\3\2\2\2\u0123"+ + "\u0126\5> \2\u0124\u0126\5<\37\2\u0125\u0123\3\2\2\2\u0125\u0124\3\2\2"+ + "\2\u0126;\3\2\2\2\u0127\u012e\7,\2\2\u0128\u0129\7,\2\2\u0129\u012a\7"+ + "\5\2\2\u012a\u012b\5 \21\2\u012b\u012c\7\6\2\2\u012c\u012e\3\2\2\2\u012d"+ + "\u0127\3\2\2\2\u012d\u0128\3\2\2\2\u012e=\3\2\2\2\u012f\u0130\7\b\2\2"+ + "\u0130\u0131\5 \21\2\u0131\u0132\7\t\2\2\u0132\u0136\3\2\2\2\u0133\u0136"+ + "\5@!\2\u0134\u0136\5B\"\2\u0135\u012f\3\2\2\2\u0135\u0133\3\2\2\2\u0135"+ + "\u0134\3\2\2\2\u0136?\3\2\2\2\u0137\u0138\7,\2\2\u0138\u013e\7\b\2\2\u0139"+ + "\u013a\5 \21\2\u013a\u013b\7\3\2\2\u013b\u013d\3\2\2\2\u013c\u0139\3\2"+ + "\2\2\u013d\u0140\3\2\2\2\u013e\u013c\3\2\2\2\u013e\u013f\3\2\2\2\u013f"+ + "\u0142\3\2\2\2\u0140\u013e\3\2\2\2\u0141\u0143\5 \21\2\u0142\u0141\3\2"+ + "\2\2\u0142\u0143\3\2\2\2\u0143\u0144\3\2\2\2\u0144\u0145\7\t\2\2\u0145"+ + "A\3\2\2\2\u0146\u0147\t\7\2\2\u0147C\3\2\2\2\35GKS]aflz\u0082\u0088\u008e"+ + "\u0097\u00a7\u00b5\u00d5\u00de\u00e8\u00f0\u00fa\u0106\u0112\u011c\u0125"+ + "\u012d\u0135\u013e\u0142"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +} \ No newline at end of file diff --git a/parser/CminusVisitor.java b/parser/CminusVisitor.java new file mode 100644 index 0000000..9088695 --- /dev/null +++ b/parser/CminusVisitor.java @@ -0,0 +1,211 @@ +// Generated from Cminus.g4 by ANTLR 4.9.1 +package parser; +import org.antlr.v4.runtime.tree.ParseTreeVisitor; + +/** + * This interface defines a complete generic visitor for a parse tree produced + * by {@link CminusParser}. + * + * @param The return type of the visit operation. Use {@link Void} for + * operations with no return type. + */ +public interface CminusVisitor extends ParseTreeVisitor { + /** + * Visit a parse tree produced by {@link CminusParser#program}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitProgram(CminusParser.ProgramContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#declaration}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitDeclaration(CminusParser.DeclarationContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#varDeclaration}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitVarDeclaration(CminusParser.VarDeclarationContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#varDeclId}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitVarDeclId(CminusParser.VarDeclIdContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#funDeclaration}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFunDeclaration(CminusParser.FunDeclarationContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#typeSpecifier}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTypeSpecifier(CminusParser.TypeSpecifierContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#param}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitParam(CminusParser.ParamContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#paramId}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitParamId(CminusParser.ParamIdContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#statement}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitStatement(CminusParser.StatementContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#compoundStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCompoundStmt(CminusParser.CompoundStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#expressionStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExpressionStmt(CminusParser.ExpressionStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#ifStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitIfStmt(CminusParser.IfStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#whileStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitWhileStmt(CminusParser.WhileStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#returnStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitReturnStmt(CminusParser.ReturnStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#breakStmt}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitBreakStmt(CminusParser.BreakStmtContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#expression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitExpression(CminusParser.ExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#simpleExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSimpleExpression(CminusParser.SimpleExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#orExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitOrExpression(CminusParser.OrExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#andExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitAndExpression(CminusParser.AndExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#unaryRelExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#relExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRelExpression(CminusParser.RelExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#relop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitRelop(CminusParser.RelopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#sumExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSumExpression(CminusParser.SumExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#sumop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitSumop(CminusParser.SumopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#termExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitTermExpression(CminusParser.TermExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#mulop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMulop(CminusParser.MulopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#unaryExpression}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitUnaryExpression(CminusParser.UnaryExpressionContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#unaryop}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitUnaryop(CminusParser.UnaryopContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#factor}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitFactor(CminusParser.FactorContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#mutable}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitMutable(CminusParser.MutableContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#immutable}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitImmutable(CminusParser.ImmutableContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#call}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitCall(CminusParser.CallContext ctx); + /** + * Visit a parse tree produced by {@link CminusParser#constant}. + * @param ctx the parse tree + * @return the visitor result + */ + T visitConstant(CminusParser.ConstantContext ctx); +} \ No newline at end of file diff --git a/submit/ASTVisitor.java b/submit/ASTVisitor.java new file mode 100644 index 0000000..b8762b5 --- /dev/null +++ b/submit/ASTVisitor.java @@ -0,0 +1,304 @@ +package submit; + +import org.antlr.v4.runtime.tree.ParseTree; +import org.antlr.v4.runtime.tree.TerminalNode; +import parser.CminusBaseVisitor; +import parser.CminusParser; +import submit.ast.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +public class ASTVisitor extends CminusBaseVisitor { + private final Logger LOGGER; + private SymbolTable symbolTable; + + public ASTVisitor(Logger LOGGER) { + this.LOGGER = LOGGER; + } + + public SymbolTable getSymbolTable() { + return symbolTable; + } + + private VarType getVarType(CminusParser.TypeSpecifierContext ctx) { + final String t = ctx.getText(); + return (t.equals("int")) ? VarType.INT : (t.equals("bool")) ? VarType.BOOL : VarType.CHAR; + } + + @Override + public Node visitProgram(CminusParser.ProgramContext ctx) { + symbolTable = new SymbolTable(); + List decls = new ArrayList<>(); + for (CminusParser.DeclarationContext d : ctx.declaration()) { + decls.add((Declaration) visitDeclaration(d)); + } + return new Program(decls); + } + + @Override + public Node visitVarDeclaration(CminusParser.VarDeclarationContext ctx) { + VarType type = getVarType(ctx.typeSpecifier()); + List ids = new ArrayList<>(); + List arraySizes = new ArrayList<>(); + for (CminusParser.VarDeclIdContext v : ctx.varDeclId()) { + String id = v.ID().getText(); + ids.add(id); + symbolTable.addSymbol(id, new SymbolInfo(id, type, false)); + if (v.NUMCONST() != null) { + arraySizes.add(Integer.parseInt(v.NUMCONST().getText())); + } else { + arraySizes.add(-1); + } + } + final boolean isStatic = false; + return new VarDeclaration(type, ids, arraySizes, isStatic); + } + + @Override + public Node visitFunDeclaration(CminusParser.FunDeclarationContext ctx) { + VarType returnType = null; + if (ctx.typeSpecifier() != null) { + returnType = getVarType(ctx.typeSpecifier()); + } + String id = ctx.ID().getText(); + List params = new ArrayList<>(); + for (CminusParser.ParamContext p : ctx.param()) { + params.add((Param) visitParam(p)); + } + Statement statement = (Statement) visitStatement(ctx.statement()); + symbolTable.addSymbol(id, new SymbolInfo(id, returnType, true)); + return new FunDeclaration(returnType, id, params, statement); + } + + @Override + public Node visitParam(CminusParser.ParamContext ctx) { + VarType type = getVarType(ctx.typeSpecifier()); + String id = ctx.paramId().ID().getText(); + symbolTable.addSymbol(id, new SymbolInfo(id, type, false)); + return new Param(type, id, ctx.paramId().children.size() > 1); + } + + @Override + public Node visitCompoundStmt(CminusParser.CompoundStmtContext ctx) { + symbolTable = symbolTable.createChild(); + List statements = new ArrayList<>(); + for (CminusParser.VarDeclarationContext d : ctx.varDeclaration()) { + statements.add((VarDeclaration) visitVarDeclaration(d)); + } + for (CminusParser.StatementContext d : ctx.statement()) { + statements.add((Statement) visitStatement(d)); + } + symbolTable = symbolTable.getParent(); + return new CompoundStatement(statements); + } + + @Override + public Node visitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { + if (ctx.expression() == null) { + return Statement.empty(); + } + return new ExpressionStatement((Expression) visitExpression(ctx.expression())); + } + + @Override + public Node visitIfStmt(CminusParser.IfStmtContext ctx) { + Expression expression = (Expression) visitSimpleExpression(ctx.simpleExpression()); + Statement trueStatement = (Statement) visitStatement(ctx.statement(0)); + Statement falseStatement = null; + if (ctx.statement().size() > 1) { + falseStatement = (Statement) visitStatement(ctx.statement(1)); + } + return new If(expression, trueStatement, falseStatement); + } + + @Override + public Node visitWhileStmt(CminusParser.WhileStmtContext ctx) { + Expression expression = (Expression) visitSimpleExpression(ctx.simpleExpression()); + Statement statement = (Statement) visitStatement(ctx.statement()); + return new While(expression, statement); + } + + @Override + public Node visitReturnStmt(CminusParser.ReturnStmtContext ctx) { + if (ctx.expression() != null) { + return new Return((Expression) visitExpression(ctx.expression())); + } + return new Return(null); + } + + @Override + public Node visitBreakStmt(CminusParser.BreakStmtContext ctx) { + return new Break(); + } + + @Override + public Node visitExpression(CminusParser.ExpressionContext ctx) { + final Node ret; + CminusParser.MutableContext mutable = ctx.mutable(); + CminusParser.ExpressionContext expression = ctx.expression(); + if (mutable != null) { + // Assignment + ParseTree operator = ctx.getChild(1); + Mutable lhs = (Mutable) visitMutable(mutable);// new Mutable(mutable.ID().getText(), (Expression) + // visitExpression(mutable.expression())); + Expression rhs = null; + if (expression != null) { + rhs = (Expression) visitExpression(expression); + } + ret = new Assignment(lhs, operator.getText(), rhs); + } else { + ret = visitSimpleExpression(ctx.simpleExpression()); + } + return ret; + } + + @Override + public Node visitOrExpression(CminusParser.OrExpressionContext ctx) { + List ands = new ArrayList<>(); + for (CminusParser.AndExpressionContext and : ctx.andExpression()) { + ands.add(visitAndExpression(and)); + } + if (ands.size() == 1) { + return ands.get(0); + } + BinaryOperator op = new BinaryOperator((Expression) ands.get(0), "||", (Expression) ands.get(1)); + for (int i = 2; i < ands.size(); ++i) { + op = new BinaryOperator(op, "||", (Expression) ands.get(i)); + } + return op; + } + + @Override + public Node visitAndExpression(CminusParser.AndExpressionContext ctx) { + List uns = new ArrayList<>(); + for (CminusParser.UnaryRelExpressionContext un : ctx.unaryRelExpression()) { + uns.add(visitUnaryRelExpression(un)); + } + if (uns.size() == 1) { + return uns.get(0); + } + BinaryOperator op = new BinaryOperator((Expression) uns.get(0), "&&", (Expression) uns.get(1)); + for (int i = 2; i < uns.size(); ++i) { + op = new BinaryOperator(op, "&&", (Expression) uns.get(i)); + } + return op; + } + + @Override + public Node visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { + Expression e = (Expression) visitRelExpression(ctx.relExpression()); + for (TerminalNode n : ctx.BANG()) { + e = new UnaryOperator("!", e); + } + return e; + } + + @Override + public Node visitRelExpression(CminusParser.RelExpressionContext ctx) { + List uns = new ArrayList<>(); + for (CminusParser.SumExpressionContext un : ctx.sumExpression()) { + uns.add(visitSumExpression(un)); + } + if (uns.size() == 1) { + return uns.get(0); + } + BinaryOperator op = new BinaryOperator((Expression) uns.get(0), ctx.relop(0).getText(), + (Expression) uns.get(1)); + for (int i = 2; i < uns.size(); ++i) { + op = new BinaryOperator(op, ctx.relop(i - 1).getText(), (Expression) uns.get(i)); + } + return op; + } + + @Override + public Node visitSumExpression(CminusParser.SumExpressionContext ctx) { + List es = new ArrayList<>(); + for (CminusParser.TermExpressionContext e : ctx.termExpression()) { + es.add(visitTermExpression(e)); + } + if (es.size() == 1) { + return es.get(0); + } + BinaryOperator op = new BinaryOperator((Expression) es.get(0), ctx.sumop(0).getText(), (Expression) es.get(1)); + for (int i = 2; i < es.size(); ++i) { + op = new BinaryOperator(op, ctx.sumop(i - 1).getText(), (Expression) es.get(i)); + } + return op; + } + + @Override + public Node visitTermExpression(CminusParser.TermExpressionContext ctx) { + List es = new ArrayList<>(); + for (CminusParser.UnaryExpressionContext e : ctx.unaryExpression()) { + es.add(visitUnaryExpression(e)); + } + if (es.size() == 1) { + return es.get(0); + } + BinaryOperator op = new BinaryOperator((Expression) es.get(0), ctx.mulop(0).getText(), (Expression) es.get(1)); + for (int i = 2; i < es.size(); ++i) { + op = new BinaryOperator(op, ctx.mulop(i - 1).getText(), (Expression) es.get(i)); + } + return op; + } + + @Override + public Node visitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { + Node ret = visitFactor(ctx.factor()); + for (int i = ctx.unaryop().size() - 1; i >= 0; i--) { + ret = new UnaryOperator(ctx.unaryop(i).getText(), (Expression) ret); + } + return ret; + } + + @Override + public Node visitMutable(CminusParser.MutableContext ctx) { + Expression e = null; + if (ctx.expression() != null) { + e = (Expression) visitExpression(ctx.expression()); + } + String id = ctx.ID().getText(); + if (symbolTable.find(id) == null) { + LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + ": " + id); + } + return new Mutable(id, e); + } + + @Override + public Node visitImmutable(CminusParser.ImmutableContext ctx) { + if (ctx.expression() != null) { + return new ParenExpression((Expression) visitExpression(ctx.expression())); + } + return visitChildren(ctx); + } + + @Override + public Node visitCall(CminusParser.CallContext ctx) { + final String id = ctx.ID().getText(); + final List args = new ArrayList<>(); + for (CminusParser.ExpressionContext e : ctx.expression()) { + args.add((Expression) visitExpression(e)); + } + if (symbolTable.find(id) == null) { + LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + ": " + id); + } + return new Call(id, args); + } + + @Override + public Node visitConstant(CminusParser.ConstantContext ctx) { + final Node node; + if (ctx.NUMCONST() != null) { + node = new NumConstant(Integer.parseInt(ctx.NUMCONST().getText())); + } else if (ctx.CHARCONST() != null) { + node = new CharConstant(ctx.CHARCONST().getText().charAt(0)); + } else if (ctx.STRINGCONST() != null) { + node = new StringConstant(ctx.STRINGCONST().getText()); + } else { + node = new BoolConstant(ctx.getText().equals("true")); + } + return node; + } +} diff --git a/submit/MIPSResult.java b/submit/MIPSResult.java new file mode 100644 index 0000000..212719e --- /dev/null +++ b/submit/MIPSResult.java @@ -0,0 +1,61 @@ +/* + */ +package submit; + +import submit.ast.VarType; + +/** + * This class represents the result of generating MIPS code. There are various + * forms this result can take: 1) void, for cases where the calling node doesn't + * need any information returned, such as a return statement. 2) register, for + * cases where the called node needs to inform the calling node what register a + * result is placed in, such as BinaryOperator. 3) address, for cases where the + * returning result is in memory, such as StringConstant. + * + * To instantiate a MIPSResult object use the factory methods create...(). + * + */ +public class MIPSResult { + + private final String register; + private final String address; + private final VarType type; + + public static MIPSResult createVoidResult() { + return new MIPSResult(null, null, null); + } + + public static MIPSResult createRegisterResult(String register, VarType type) { + return new MIPSResult(register, null, type); + } + + public static MIPSResult createAddressResult(String address, VarType type) { + return new MIPSResult(null, address, type); + } + + private MIPSResult(String register, String address, VarType type) { + this.register = register; + this.address = address; + this.type = type; + } + + /** + * Anytime you get a register from a result you should seriously consider + * calling regAllocator.clear(reg) after using the register to minimize + * register usage. + * + * @return + */ + public String getRegister() { + return register; + } + + public String getAddress() { + return address; + } + + public VarType getType() { + return type; + } + +} diff --git a/submit/RegisterAllocator.java b/submit/RegisterAllocator.java new file mode 100644 index 0000000..ce18b2b --- /dev/null +++ b/submit/RegisterAllocator.java @@ -0,0 +1,126 @@ +/* + */ +package submit; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * + * + * + */ +public final class RegisterAllocator { + + // True if t is used + private final boolean[] t = new boolean[10]; + private final boolean[] s = new boolean[8]; + private final Set used = new HashSet<>(); + + public RegisterAllocator() { + clearAll(); + } + + public String getT() { + for (int i = 0; i < t.length; ++i) { + if (!t[i]) { + t[i] = true; + String str = "$t" + i; + used.add(str); + return str; + } + } + return null; + } + +// public String getS() { +// for (int i = 0; i < s.length; ++i) { +// if (!s[i]) { +// s[i] = true; +// String str = "$s" + i; +// used.add(str); +// return str; +// } +// } +// return null; +// } + + // Returns the number of bytes used to save the registers + public int saveRestore(StringBuilder code, int baseOffset, boolean s_or_t, boolean save) { + boolean[] r = s; + String prefix = "$s"; + if (!s_or_t) { + r = t; + prefix = "$t"; + } + String instruction = "sw"; + if (!save) { + instruction = "lw"; + } + int offset = 0; + for (int i = 0; i < r.length; ++i) { + if (r[i]) { + offset -= 4; + String str = prefix + i; + code.append(instruction).append(" ").append(str).append(" ").append(offset-baseOffset).append("($sp)\n"); + } + } + return -offset; + } + +// public int saveS(StringBuilder code, int baseOffset) { +// return saveRestore(code, baseOffset, true, true); +// } + + public int saveT(StringBuilder code, int baseOffset) { + return saveRestore(code, baseOffset, false, true); + } + +// public int restoreS(StringBuilder code, int baseOffset) { +// return saveRestore(code, baseOffset, true, false); +// } + + public int restoreT(StringBuilder code, int baseOffset) { + return saveRestore(code, baseOffset, false, false); + } + + public List getUsed() { + return new ArrayList<>(used); + } + + /** + * Any time you call this method you should seriously consider adding a + * corresponding clear() call. + * + * @return + */ + public String getAny() { +// String availS = getS(); +// if (availS != null) { +// return availS; +// } + String t = getT(); + if (t == null) { + throw new RuntimeException("Out of registers"); + } + return t; + } + + public void clear(String reg) { + if (reg.charAt(1) == 's') { + s[Integer.parseInt(reg.substring(2))] = false; + } else if (reg.charAt(1) == 't') { + t[Integer.parseInt(reg.substring(2))] = false; + } else { + throw new RuntimeException("Unexpected register in clear"); + } + } + + public void clearAll() { + Arrays.fill(t, false); + Arrays.fill(s, false); + } +} diff --git a/submit/SymbolInfo.java b/submit/SymbolInfo.java new file mode 100644 index 0000000..19c3f9b --- /dev/null +++ b/submit/SymbolInfo.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit; + +import submit.ast.VarType; + +/** + * + * @author edwajohn + */ +public class SymbolInfo { + + private final String id; + // In the case of a function, type is the return type + private final VarType type; + private final boolean function; + + public SymbolInfo(String id, VarType type, boolean function) { + this.id = id; + this.type = type; + this.function = function; + } + + @Override + public String toString() { + return "<" + id + ", " + type + '>'; + } + +} diff --git a/submit/SymbolTable.java b/submit/SymbolTable.java new file mode 100644 index 0000000..7ca6b27 --- /dev/null +++ b/submit/SymbolTable.java @@ -0,0 +1,63 @@ +package submit; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/* + * Code formatter project + * CS 4481 + */ +/** + * + */ +public class SymbolTable { + + private final HashMap table; + private SymbolTable parent; + private final List children; + + public SymbolTable() { + table = new HashMap<>(); + parent = null; + children = new ArrayList<>(); + } + + public void addSymbol(String id, SymbolInfo symbol) { + table.put(id, symbol); + } + + /** + * Returns null if no symbol with that id is in this symbol table or an + * ancestor table. + * + * @param id + * @return + */ + public SymbolInfo find(String id) { + if (table.containsKey(id)) { + return table.get(id); + } + if (parent != null) { + return parent.find(id); + } + return null; + } + + /** + * Returns the new child. + * + * @return + */ + public SymbolTable createChild() { + SymbolTable child = new SymbolTable(); + children.add(child); + child.parent = this; + return child; + } + + public SymbolTable getParent() { + return parent; + } + +} diff --git a/submit/ast/AbstractNode.java b/submit/ast/AbstractNode.java new file mode 100644 index 0000000..01719aa --- /dev/null +++ b/submit/ast/AbstractNode.java @@ -0,0 +1,15 @@ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +public class AbstractNode implements Node { + public void toCminus(StringBuilder sb, String prefix) { + } + + public MIPSResult toMIPS(StringBuilder code, StringBuilder data, SymbolTable symbolTable, + RegisterAllocator regAllocator) { + return null; + } +} diff --git a/submit/ast/Assignment.java b/submit/ast/Assignment.java new file mode 100644 index 0000000..1a5d172 --- /dev/null +++ b/submit/ast/Assignment.java @@ -0,0 +1,33 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Assignment extends AbstractNode implements Expression { + + private final Mutable mutable; + private final AssignmentType type; + private final Expression rhs; + + public Assignment(Mutable mutable, String assign, Expression rhs) { + this.mutable = mutable; + this.type = AssignmentType.fromString(assign); + this.rhs = rhs; + } + + public void toCminus(StringBuilder builder, final String prefix) { + mutable.toCminus(builder, prefix); + if (rhs != null) { + builder.append(" ").append(type.toString()).append(" "); + rhs.toCminus(builder, prefix); + } else { + builder.append(type.toString()); + } + } + +} diff --git a/submit/ast/AssignmentType.java b/submit/ast/AssignmentType.java new file mode 100644 index 0000000..9dd2bdf --- /dev/null +++ b/submit/ast/AssignmentType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum AssignmentType { + + EQUALS("="), PLUS("+="), MINUS("-="), TIMES("*="), DIVIDE("/="), INC("++"), DEC("--"); + + private final String value; + + private AssignmentType(String value) { + this.value = value; + } + + public static AssignmentType fromString(String s) { + for (AssignmentType at : AssignmentType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in AssignType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/submit/ast/BinaryOperator.java b/submit/ast/BinaryOperator.java new file mode 100644 index 0000000..d18766b --- /dev/null +++ b/submit/ast/BinaryOperator.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class BinaryOperator extends AbstractNode implements Expression { + + private final Expression lhs, rhs; + private final BinaryOperatorType type; + + public BinaryOperator(Expression lhs, BinaryOperatorType type, Expression rhs) { + this.lhs = lhs; + this.type = type; + this.rhs = rhs; + } + + public BinaryOperator(Expression lhs, String type, Expression rhs) { + this.lhs = lhs; + this.type = BinaryOperatorType.fromString(type); + this.rhs = rhs; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + lhs.toCminus(builder, prefix); + builder.append(" ").append(type).append(" "); + rhs.toCminus(builder, prefix); + } + +} diff --git a/submit/ast/BinaryOperatorType.java b/submit/ast/BinaryOperatorType.java new file mode 100644 index 0000000..37235c8 --- /dev/null +++ b/submit/ast/BinaryOperatorType.java @@ -0,0 +1,37 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum BinaryOperatorType { + + OR("||"), AND("&&"), + LE("<="), LT("<"), GT(">"), GE(">="), EQ("=="), NE("!="), + PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/"), MOD("%"); + + private final String value; + + private BinaryOperatorType(String value) { + this.value = value; + } + + public static BinaryOperatorType fromString(String s) { + for (BinaryOperatorType at : BinaryOperatorType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in OperatorType.fromString(): " + s); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/submit/ast/BoolConstant.java b/submit/ast/BoolConstant.java new file mode 100644 index 0000000..4b0ad2c --- /dev/null +++ b/submit/ast/BoolConstant.java @@ -0,0 +1,27 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class BoolConstant extends AbstractNode implements Expression { + + private final boolean value; + + public BoolConstant(boolean value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + if (value) { + builder.append("true"); + } else { + builder.append("false"); + } + } + +} diff --git a/submit/ast/Break.java b/submit/ast/Break.java new file mode 100644 index 0000000..e3f28bc --- /dev/null +++ b/submit/ast/Break.java @@ -0,0 +1,18 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Break extends AbstractNode implements Statement { + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("break;\n"); + } + +} diff --git a/submit/ast/Call.java b/submit/ast/Call.java new file mode 100644 index 0000000..b6e61ba --- /dev/null +++ b/submit/ast/Call.java @@ -0,0 +1,37 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class Call extends AbstractNode implements Expression { + + private final String id; + private final List args; + + public Call(String id, List args) { + this.id = id; + this.args = new ArrayList<>(args); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(id).append("("); + for (Expression arg : args) { + arg.toCminus(builder, prefix); + builder.append(", "); + } + if (!args.isEmpty()) { + builder.setLength(builder.length() - 2); + } + builder.append(")"); + } + +} diff --git a/submit/ast/CharConstant.java b/submit/ast/CharConstant.java new file mode 100644 index 0000000..8ca801d --- /dev/null +++ b/submit/ast/CharConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class CharConstant extends AbstractNode implements Expression { + + private final char value; + + public CharConstant(char value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append("'").append(value).append("'"); + } + +} diff --git a/submit/ast/CompoundStatement.java b/submit/ast/CompoundStatement.java new file mode 100644 index 0000000..bd5628a --- /dev/null +++ b/submit/ast/CompoundStatement.java @@ -0,0 +1,30 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.List; + +/** + * + * @author edwajohn + */ +public class CompoundStatement extends AbstractNode implements Statement { + + private final List statements; + + public CompoundStatement(List statements) { + this.statements = statements; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("{\n"); + for (Statement s : statements) { + s.toCminus(builder, prefix + " "); + } + builder.append(prefix).append("}\n"); + } + +} diff --git a/submit/ast/Declaration.java b/submit/ast/Declaration.java new file mode 100644 index 0000000..32947e6 --- /dev/null +++ b/submit/ast/Declaration.java @@ -0,0 +1,13 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public interface Declaration extends Statement { + +} diff --git a/submit/ast/Expression.java b/submit/ast/Expression.java new file mode 100644 index 0000000..910f6f0 --- /dev/null +++ b/submit/ast/Expression.java @@ -0,0 +1,13 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public interface Expression extends Node { + +} diff --git a/submit/ast/ExpressionStatement.java b/submit/ast/ExpressionStatement.java new file mode 100644 index 0000000..69071d8 --- /dev/null +++ b/submit/ast/ExpressionStatement.java @@ -0,0 +1,26 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class ExpressionStatement extends AbstractNode implements Statement { + + private final Expression expression; + + public ExpressionStatement(Expression expression) { + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + expression.toCminus(builder, prefix); + builder.append(";\n"); + } + +} diff --git a/submit/ast/FunDeclaration.java b/submit/ast/FunDeclaration.java new file mode 100644 index 0000000..1ec2ca6 --- /dev/null +++ b/submit/ast/FunDeclaration.java @@ -0,0 +1,46 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class FunDeclaration extends AbstractNode implements Declaration { + + private final VarType returnType; + private final String id; + private final ArrayList params; + private final Statement statement; + + public FunDeclaration(VarType returnType, String id, List params, + Statement statement) { + this.returnType = returnType; + this.id = id; + this.params = new ArrayList<>(params); + this.statement = statement; + } + + public void toCminus(StringBuilder builder, final String prefix) { + String rt = (returnType != null) ? returnType.toString() : "void"; + builder.append("\n").append(rt).append(" "); + builder.append(id); + builder.append("("); + + for (Param param : params) { + param.toCminus(builder, prefix); + builder.append(", "); + } + if (!params.isEmpty()) { + builder.delete(builder.length() - 2, builder.length()); + } + builder.append(")\n"); + statement.toCminus(builder, prefix); + } + +} diff --git a/submit/ast/If.java b/submit/ast/If.java new file mode 100644 index 0000000..a516c94 --- /dev/null +++ b/submit/ast/If.java @@ -0,0 +1,44 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class If extends AbstractNode implements Statement { + + private final Expression expression; + private final Statement trueStatement; + private final Statement falseStatement; + + public If(Expression expression, Statement trueStatement, Statement falseStatement) { + this.expression = expression; + this.trueStatement = trueStatement; + this.falseStatement = falseStatement; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("if ("); + expression.toCminus(builder, prefix); + builder.append(")\n"); + if (trueStatement instanceof CompoundStatement) { + trueStatement.toCminus(builder, prefix); + } else { + trueStatement.toCminus(builder, prefix + " "); + } + if (falseStatement != null) { + builder.append(prefix).append("else\n"); + // falseStatement.toCminus(builder, prefix); + if (falseStatement instanceof CompoundStatement) { + falseStatement.toCminus(builder, prefix); + } else { + falseStatement.toCminus(builder, prefix + " "); + } + } + // builder.append(prefix).append("}"); + } +} diff --git a/submit/ast/Mutable.java b/submit/ast/Mutable.java new file mode 100644 index 0000000..d549371 --- /dev/null +++ b/submit/ast/Mutable.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Mutable extends AbstractNode implements Expression { + + private final String id; + private final Expression index; + + public Mutable(String id, Expression index) { + this.id = id; + this.index = index; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(id); + if (index != null) { + builder.append("["); + index.toCminus(builder, prefix); + builder.append("]"); + } + } + +} diff --git a/submit/ast/Node.java b/submit/ast/Node.java new file mode 100644 index 0000000..2eed788 --- /dev/null +++ b/submit/ast/Node.java @@ -0,0 +1,11 @@ +package submit.ast; + +import submit.MIPSResult; +import submit.RegisterAllocator; +import submit.SymbolTable; + +public interface Node { + void toCminus(StringBuilder builder, final String prefix); + + MIPSResult toMIPS(StringBuilder code, StringBuilder data, SymbolTable symbolTable, RegisterAllocator regAllocator); +} diff --git a/submit/ast/NumConstant.java b/submit/ast/NumConstant.java new file mode 100644 index 0000000..4e343fd --- /dev/null +++ b/submit/ast/NumConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class NumConstant extends AbstractNode implements Expression { + + private final int value; + + public NumConstant(int value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(Integer.toString(value)); + } + +} diff --git a/submit/ast/Param.java b/submit/ast/Param.java new file mode 100644 index 0000000..478c6e2 --- /dev/null +++ b/submit/ast/Param.java @@ -0,0 +1,43 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Param extends AbstractNode implements Node { + + private final VarType type; + private final String id; + private final boolean array; + + public Param(VarType type, String id, boolean array) { + this.type = type; + this.id = id; + this.array = array; + } + + public VarType getType() { + return type; + } + + public String getId() { + return id; + } + + public boolean isArray() { + return array; + } + + public void toCminus(StringBuilder builder, final String prefix) { + if (isArray()) { + builder.append(type).append(" ").append(id).append("[]"); + } else { + builder.append(type).append(" ").append(id); + } + } + +} diff --git a/submit/ast/ParenExpression.java b/submit/ast/ParenExpression.java new file mode 100644 index 0000000..2eb0b8f --- /dev/null +++ b/submit/ast/ParenExpression.java @@ -0,0 +1,26 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class ParenExpression extends AbstractNode implements Expression { + + private final Expression expression; + + public ParenExpression(Expression expression) { + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append("("); + expression.toCminus(builder, prefix); + builder.append(")"); + } + +} diff --git a/submit/ast/Program.java b/submit/ast/Program.java new file mode 100644 index 0000000..1478961 --- /dev/null +++ b/submit/ast/Program.java @@ -0,0 +1,36 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class Program extends AbstractNode implements Node { + + private ArrayList declarations; + + public Program(List declarations) { + this.declarations = new ArrayList<>(declarations); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + toCminus(builder, ""); + return builder.toString(); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + for (Declaration declaration : declarations) { + declaration.toCminus(builder, ""); + } + } + +} diff --git a/submit/ast/Return.java b/submit/ast/Return.java new file mode 100644 index 0000000..47a8c16 --- /dev/null +++ b/submit/ast/Return.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Return extends AbstractNode implements Statement { + + private final Expression expr; + + public Return(Expression expr) { + this.expr = expr; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + if (expr == null) { + builder.append("return;\n"); + } else { + builder.append("return "); + expr.toCminus(builder, prefix); + builder.append(";\n"); + } + } + +} diff --git a/submit/ast/Statement.java b/submit/ast/Statement.java new file mode 100644 index 0000000..2e0581e --- /dev/null +++ b/submit/ast/Statement.java @@ -0,0 +1,16 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; + +/** + * + * @author edwajohn + */ +public interface Statement extends Node { + public static CompoundStatement empty() { return new CompoundStatement(new ArrayList<>()); } + +} diff --git a/submit/ast/StringConstant.java b/submit/ast/StringConstant.java new file mode 100644 index 0000000..c3b966e --- /dev/null +++ b/submit/ast/StringConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class StringConstant extends AbstractNode implements Expression { + + private final String value; + + public StringConstant(String value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append("\"").append(value).append("\""); + } + +} diff --git a/submit/ast/UnaryOperator.java b/submit/ast/UnaryOperator.java new file mode 100644 index 0000000..1b5082a --- /dev/null +++ b/submit/ast/UnaryOperator.java @@ -0,0 +1,27 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class UnaryOperator extends AbstractNode implements Expression { + + private final UnaryOperatorType type; + private final Expression expression; + + public UnaryOperator(String type, Expression expression) { + this.type = UnaryOperatorType.fromString(type); + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(type); + expression.toCminus(builder, prefix); + } + +} diff --git a/submit/ast/UnaryOperatorType.java b/submit/ast/UnaryOperatorType.java new file mode 100644 index 0000000..828ee5a --- /dev/null +++ b/submit/ast/UnaryOperatorType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum UnaryOperatorType { + + NOT("!"), NEG("-"), DEREF("*"), QUESTION("?"); + + private final String value; + + private UnaryOperatorType(String value) { + this.value = value; + } + + public static UnaryOperatorType fromString(String s) { + for (UnaryOperatorType at : UnaryOperatorType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in UnaryOperatorType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/submit/ast/VarDeclaration.java b/submit/ast/VarDeclaration.java new file mode 100644 index 0000000..d2a5264 --- /dev/null +++ b/submit/ast/VarDeclaration.java @@ -0,0 +1,47 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class VarDeclaration extends AbstractNode implements Declaration { + + private final VarType type; + private final List ids; + private final List arraySizes; + private final boolean isStatic; + + public VarDeclaration(VarType type, List ids, List arraySizes, boolean isStatic) { + this.type = type; + this.ids = new ArrayList<>(ids); + this.arraySizes = arraySizes; + this.isStatic = isStatic; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(prefix); + if (isStatic) { + builder.append("static "); + } + builder.append(type).append(" "); + for (int i = 0; i < ids.size(); ++i) { + final String id = ids.get(i); + final int arraySize = arraySizes.get(i); + if (arraySize >= 0) { + builder.append(id).append("[").append(arraySize).append("]").append(", "); + } else { + builder.append(id).append(", "); + } + } + builder.delete(builder.length() - 2, builder.length()); + builder.append(";\n"); + } + +} diff --git a/submit/ast/VarType.java b/submit/ast/VarType.java new file mode 100644 index 0000000..ba28946 --- /dev/null +++ b/submit/ast/VarType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum VarType { + + INT("int"), BOOL("bool"), CHAR("char"); + + private final String value; + + private VarType(String value) { + this.value = value; + } + + public static VarType fromString(String s) { + for (VarType vt : VarType.values()) { + if (vt.value.equals(s)) { + return vt; + } + } + throw new RuntimeException("Illegal string in VarType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/submit/ast/While.java b/submit/ast/While.java new file mode 100644 index 0000000..a37195e --- /dev/null +++ b/submit/ast/While.java @@ -0,0 +1,33 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class While extends AbstractNode implements Statement { + + private final Expression expression; + private final Statement statement; + + public While(Expression expression, Statement statement) { + this.expression = expression; + this.statement = statement; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("while ("); + expression.toCminus(builder, prefix); + builder.append(")\n"); + if (statement instanceof CompoundStatement) { + statement.toCminus(builder, prefix); + } else { + statement.toCminus(builder, prefix + " "); + } + + } +}