Skip to content

Commit

Permalink
input instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
imwithye committed Feb 25, 2016
1 parent 075c11b commit 1378b13
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
7 changes: 5 additions & 2 deletions example/fibonacci.ly
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ func fibonacci(n) {
}

func main() {
appender = "The fibonacci number is "
print(appender + string(fibonacci(10)))
text = "input a number: "
print(text)
n = number(input())
text = "The fibonacci number of " + string(n) + " is " + string(fibonacci(10))
print(text)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.lucylang.ljvm.machine.instruction;

import org.lucylang.ljvm.machine.Machine;
import org.lucylang.ljvm.machine.Register;
import org.lucylang.ljvm.machine.module.Module;
import org.lucylang.ljvm.scope.OverdefinedException;
import org.lucylang.ljvm.scope.UndefinedException;
import org.lucylang.ljvm.type.TypeUnmatchedException;
import org.lucylang.ljvm.value.StringValue;
import org.lucylang.ljvm.value.ValueUnavailableException;

import java.util.ArrayList;
import java.util.Scanner;

public class GetInstruction extends Instruction {
public GetInstruction(Operand value) {
this.type = Type.GET;
this.operands = new ArrayList<Operand>();
this.operands.add(value);

this.validSize = 1;
this.validRefs = new int[]{0};
}

@Override
public boolean executeValid(Machine vm, Module module) throws InvalidInstruction, TypeUnmatchedException, ValueUnavailableException, UndefinedException, OverdefinedException {
Register r = this.getRegister(vm, 0);
Scanner scanner = new Scanner(System.in);
r.assignValue(new StringValue(scanner.next()));
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ public enum Type {
PUSH, POP, PEEK, CALL, RET,
GOTO, BEQ, BNE,
NUM, STR, BOOL,
PUT
PUT, GET
}
6 changes: 6 additions & 0 deletions src/main/java/org/lucylang/ljvm/machine/module/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class Module implements Serializable {

public Module() {
this.routines = new Scope<String, Routine>("module");
Routine input = new Routine(new Instruction[]{
new GetInstruction(new RefOperand("value")),
new PushInstruction(new RefOperand("value")),
new RetInstruction()
});
Routine print = new Routine(new Instruction[]{
new PopInstruction(new RefOperand("value")),
new PutInstruction(new RefOperand("value"))
Expand All @@ -34,6 +39,7 @@ public Module() {
new PushInstruction(new RefOperand("$1")),
new RetInstruction()
});
this.routines.set("input", input);
this.routines.set("print", print);
this.routines.set("string", string);
this.routines.set("number", number);
Expand Down

0 comments on commit 1378b13

Please sign in to comment.