diff --git a/example/fibonacci.ly b/example/fibonacci.ly index ecef1b8..1c1dfc4 100644 --- a/example/fibonacci.ly +++ b/example/fibonacci.ly @@ -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) } diff --git a/src/main/java/org/lucylang/ljvm/machine/instruction/GetInstruction.java b/src/main/java/org/lucylang/ljvm/machine/instruction/GetInstruction.java new file mode 100644 index 0000000..305ae80 --- /dev/null +++ b/src/main/java/org/lucylang/ljvm/machine/instruction/GetInstruction.java @@ -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(); + 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; + } +} diff --git a/src/main/java/org/lucylang/ljvm/machine/instruction/Type.java b/src/main/java/org/lucylang/ljvm/machine/instruction/Type.java index 49e8efc..72d3d1e 100644 --- a/src/main/java/org/lucylang/ljvm/machine/instruction/Type.java +++ b/src/main/java/org/lucylang/ljvm/machine/instruction/Type.java @@ -5,5 +5,5 @@ public enum Type { PUSH, POP, PEEK, CALL, RET, GOTO, BEQ, BNE, NUM, STR, BOOL, - PUT + PUT, GET } diff --git a/src/main/java/org/lucylang/ljvm/machine/module/Module.java b/src/main/java/org/lucylang/ljvm/machine/module/Module.java index 13c3ab6..d4702d5 100644 --- a/src/main/java/org/lucylang/ljvm/machine/module/Module.java +++ b/src/main/java/org/lucylang/ljvm/machine/module/Module.java @@ -12,6 +12,11 @@ public class Module implements Serializable { public Module() { this.routines = new Scope("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")) @@ -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);