-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eddie
authored and
Eddie
committed
Jun 10, 2024
1 parent
ba3d55a
commit eb5621e
Showing
10 changed files
with
128 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
# Compile the Java class used by our program | ||
# `javac programs/classes/com/example/MultitaskRunnable.java` | ||
|
||
# Declare or provide the jungle classpath before compiling or running, | ||
# so that our Runnable class can be validated during compile and found during runtime. | ||
# The jungle classpath is used to declare dependencies to compile the program and run the program. | ||
# `export JUNGLE_CLASSPATH='.:./programs/classes'` | ||
|
||
# Compile and run the program | ||
# `cat programs/multitask.source | jungle run` | ||
|
||
multitask "com.example.MultitaskRunnable" | ||
print "Hello" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sleep 1000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ public enum NodeType { | |
ASSERT, | ||
PRINT, | ||
MULTITASK, | ||
SLEEP, | ||
|
||
TRUE, | ||
FALSE; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/com/jungle/compiler/visitor/SleepVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.jungle.compiler.visitor; | ||
|
||
import static org.objectweb.asm.Opcodes.I2L; | ||
import static org.objectweb.asm.Opcodes.INVOKESTATIC; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.objectweb.asm.MethodVisitor; | ||
|
||
import com.jungle.ast.INode; | ||
import com.jungle.ast.NodeType; | ||
import com.jungle.compiler.ICompilerOptions; | ||
import com.jungle.compiler.operand.OperandStackContext; | ||
import com.jungle.compiler.operand.OperandStackType; | ||
|
||
public class SleepVisitor extends AbstractVisitor { | ||
|
||
@Nullable | ||
private ExpressionVisitor expressionVisitor; | ||
|
||
@NotNull | ||
private ExpressionVisitor getExpressionVisitor() { | ||
if (expressionVisitor == null) { | ||
expressionVisitor = new ExpressionVisitor(getCompilerOptions()); | ||
} | ||
return expressionVisitor; | ||
} | ||
|
||
public SleepVisitor(@NotNull ICompilerOptions options) { | ||
super(options); | ||
} | ||
|
||
@Override | ||
public boolean canVisit(@NotNull INode ast) { | ||
return ast.getType().equals(NodeType.SLEEP); | ||
} | ||
|
||
@Override | ||
public void visit(@NotNull MethodVisitor mv, @NotNull INode ast, @NotNull OperandStackContext context) { | ||
// generate code for the integer representing the number of milliseconds to wait | ||
|
||
if (ast.getLeft() == null) { | ||
throw new Error("sleep missing expression"); | ||
} | ||
|
||
// push expression onto operand stack | ||
getExpressionVisitor().visit(mv, ast.getLeft(), context); | ||
|
||
// verify that the expression was an integer type | ||
if (context.peek() != OperandStackType.INTEGER) { | ||
throw new Error("sleep expression expected to be type integer"); | ||
} | ||
|
||
// For now, convert the integer to long | ||
mv.visitInsn(I2L); | ||
|
||
// invoke static method: Thread.sleep() | ||
mv.visitMethodInsn( | ||
INVOKESTATIC, | ||
"java/lang/Thread", | ||
"sleep", | ||
"(J)V", | ||
false | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters