All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prompto.literal.TimeLiteral Maven / Gradle / Ivy

The newest version!
package prompto.literal;

import prompto.compiler.Flags;
import prompto.compiler.IOperand;
import prompto.compiler.MethodConstant;
import prompto.compiler.MethodInfo;
import prompto.compiler.Opcode;
import prompto.compiler.ResultInfo;
import prompto.compiler.StringConstant;
import prompto.intrinsic.PromptoTime;
import prompto.runtime.Context;
import prompto.transpiler.Transpiler;
import prompto.type.IType;
import prompto.type.TimeType;
import prompto.value.TimeValue;


public class TimeLiteral extends Literal {

	public TimeLiteral(String text) {
		super(text, parseTime(text.substring(1,text.length()-1)));
	}
	
	public TimeLiteral(PromptoTime time) {
		super("'" + time.toString() + "'", new TimeValue(time));
	}
	
	@Override
	public IType check(Context context) {
		return TimeType.instance();
	}
	
	public static TimeValue parseTime(String text) {
		return new TimeValue(PromptoTime.parse(text));
	}
	
	@Override
	public ResultInfo compile(Context context, MethodInfo method, Flags flags) {
		PromptoTime time = value.getStorableData();
		method.addInstruction(Opcode.LDC_W, new StringConstant(time.toString()));
		IOperand oper = new MethodConstant(PromptoTime.class, "parse", String.class, PromptoTime.class);
		method.addInstruction(Opcode.INVOKESTATIC, oper);
		return new ResultInfo(PromptoTime.class);
	}

	@Override
	public void declare(Transpiler transpiler) {
		transpiler.require("Period");
		transpiler.require("LocalTime");
	}
	
	@Override
	public boolean transpile(Transpiler transpiler) {
		transpiler.append("LocalTime.parse(").append(text.get()).append(")");
		return false;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy