public void HandleFileL(Action <UrclInstruction> emit, IEnumerable <string> src) { var source = string.Join(Environment.NewLine, src); try { var compiler = new LuC.Compiler(); compiler.Compile(LuC.Parser.Parse(compiler, source)); Standard.AddStandardDefaults(compiler); var emitter = new Emitters.Urcl(compiler); compiler.Emit(emitter, "Program.Main"); foreach (var inst in new UrclOptimizer { CullRedundantStackOps = true, CullCreateLabel = true, CullPragmas = true }.Optimize(emitter.Result.ToArray())) { emit(inst); } } catch (SourceError ex) { ex.GetLineAndColumn(source, out int line, out int column); throw new ParserError($"Compile Error (Ln {line}, Col {column}): {ex.Message} \"{source.Substring(ex.Start, ex.Length)}\""); } }
public Urcl(LuC.Compiler compiler) { Compiler = compiler; Emit(new UrclInstruction(Operation.COMPILER_COMMENT, new[] { "Initialize" })); Emit(new UrclInstruction(Operation.MOV, OperandType.Register, OperandA, OperandType.Register, Zero)); Emit(new UrclInstruction(Operation.MOV, OperandType.Register, OperandB, OperandType.Register, Zero)); Emit(new UrclInstruction(Operation.MOV, OperandType.Register, CallStack, OperandType.Register, Zero)); }
public static void AddStandardDefaults(this Compiler compiler) { compiler.AddDefaultNamespace(new Namespace(0, 0, string.Empty, new[] { Function.CreateNativeFunction("+", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.AddInt(); }, true), Function.CreateNativeFunction("-", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.SubtractInt(); }, true), Function.CreateNativeFunction("*", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.MultiplyInt(); }, true), Function.CreateNativeFunction("/", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.DivideInt(); }, true), Function.CreateNativeFunction("%", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.ModuloInt(); }, true), Function.CreateNativeFunction("<<", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.LeftShiftInt(); }, true), Function.CreateNativeFunction(">>", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.RightShiftInt(); }, true), Function.CreateNativeFunction("&", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.AndInt(); }, true), Function.CreateNativeFunction("|", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.OrInt(); }, true), Function.CreateNativeFunction("^", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.XorInt(); }, true), Function.CreateNativeFunction("~", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a") }, (emit, f) => { emit.NotInt(); }, true), Function.CreateNativeFunction("&&", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.AndBoolean(); }, true), Function.CreateNativeFunction("||", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.OrBoolean(); }, true), Function.CreateNativeFunction("^^", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { emit.XorBoolean(); }, true), Function.CreateNativeFunction("!", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a") }, (emit, f) => { emit.NotBoolean(); }, true), Function.CreateNativeFunction(">", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { var failed = emit.CreateLabel(); var end = emit.CreateLabel(); emit.SubtractInt(); emit.BranchIfNegative(failed); emit.BranchIfZero(failed); emit.LoadConstant(1); emit.Branch(end); emit.MarkLabel(failed); emit.LoadConstant(0); emit.MarkLabel(end); }, true), Function.CreateNativeFunction("<", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { var failed = emit.CreateLabel(); var end = emit.CreateLabel(); emit.SubtractInt(); emit.BranchIfPositive(failed); emit.LoadConstant(1); emit.Branch(end); emit.MarkLabel(failed); emit.LoadConstant(0); emit.MarkLabel(end); }, true), Function.CreateNativeFunction(">=", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { var failed = emit.CreateLabel(); var end = emit.CreateLabel(); emit.SubtractInt(); emit.BranchIfNegative(failed); emit.LoadConstant(1); emit.Branch(end); emit.MarkLabel(failed); emit.LoadConstant(0); emit.MarkLabel(end); }, true), Function.CreateNativeFunction("<=", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { var failed = emit.CreateLabel(); var success = emit.CreateLabel(); var end = emit.CreateLabel(); emit.SubtractInt(); emit.BranchIfZero(success); emit.BranchIfPositive(failed); emit.MarkLabel(success); emit.LoadConstant(1); emit.Branch(end); emit.MarkLabel(failed); emit.LoadConstant(0); emit.MarkLabel(end); }, true), Function.CreateNativeFunction("==", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { var failed = emit.CreateLabel(); var end = emit.CreateLabel(); emit.SubtractInt(); emit.BranchIfNotZero(failed); emit.LoadConstant(1); emit.Branch(end); emit.MarkLabel(failed); emit.LoadConstant(0); emit.MarkLabel(end); }, true), Function.CreateNativeFunction("!=", Compiler.NativeInt, new[] { new Field(0, 0, compiler, Compiler.NativeInt, "a"), new Field(0, 0, compiler, Compiler.NativeInt, "b"), }, (emit, f) => { var failed = emit.CreateLabel(); var end = emit.CreateLabel(); emit.SubtractInt(); emit.BranchIfZero(failed); emit.LoadConstant(1); emit.Branch(end); emit.MarkLabel(failed); emit.LoadConstant(0); emit.MarkLabel(end); }, true) })); }