Ejemplo n.º 1
0
		public Instruction (SourceLocation location, Opcode opcode, int arg)
			: this ()
		{
			OperationCode = opcode;
			Argument = arg;
			Location = location;
		}
Ejemplo n.º 2
0
		public Tokenizer (ErrorLog errorLog, string source, string file = "")
		{
			this.errorLog = errorLog;
			this.source = source;
			this.file = file;
			position = 0;
			sourceLen = source.Length;
			location = new SourceLocation (0, 0, file);
		}
Ejemplo n.º 3
0
		public void EmitInstruction (SourceLocation loc, Opcode opcode, int arg)
		{
			instructions.Add (new Instruction (loc, opcode, arg));
		}
Ejemplo n.º 4
0
		public Error (ErrorType etype, SourceLocation location, string text)
		{
			ErrorType = etype;
			Text = text;
			Location = location;
		}
Ejemplo n.º 5
0
			public DebugResponse (SourceLocation location, StackFrame frame)
			{
				Location = location;
				Frame = frame;
			}
Ejemplo n.º 6
0
		/// <summary>
		/// Initializes a new instance of the <see cref="Iodine.Compiler.Token"/> class.
		/// </summary>
		/// <param name="clazz">Token class.</param>
		/// <param name="value">Value.</param>
		/// <param name="location">Location.</param>
		public Token (TokenClass clazz, string value, SourceLocation location)
		{
			Class = clazz;
			Value = value;
			Location = location;
		}
Ejemplo n.º 7
0
		public Statement (SourceLocation location)
			: base (location)
		{
		}
Ejemplo n.º 8
0
 public void EmitInstruction(SourceLocation loc, Opcode opcode, IodineObject arg)
 {
     instructions.Add(new Instruction(loc, opcode, arg));
 }
Ejemplo n.º 9
0
		private void ExecuteInstruction ()
		{
			currentLocation = instruction.Location;
			switch (instruction.OperationCode) {
			case Opcode.Pop:
				{
					Pop ();
					break;
				}
			case Opcode.Dup:
				{
					IodineObject val = Pop ();
					Push (val);
					Push (val);
					break;
				}
			case Opcode.LoadConst:
				{
					Push (Top.Module.ConstantPool [instruction.Argument]);
					break;
				}
			case Opcode.LoadNull:
				{
					Push (IodineNull.Instance);
					break;
				}
			case Opcode.LoadSelf:
				{
					Push (Top.Self);
					break;
				}
			case Opcode.LoadTrue:
				{
					Push (IodineBool.True);
					break;
				}
			case Opcode.LoadException:
				{
					Push (lastException);
					break;
				}
			case Opcode.LoadFalse:
				{
					Push (IodineBool.False);
					break;
				}
			case Opcode.StoreLocal:
				{
					Top.StoreLocal (instruction.Argument, Pop ());
					break;
				}
			case Opcode.LoadLocal:
				{
					Push (Top.LoadLocal (instruction.Argument));
					break;
				}
			case Opcode.StoreGlobal:
				{
					string name = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
					if (Globals.ContainsKey (name)) {
						Globals [name] = Pop ();
					} else {
						Top.Module.SetAttribute (this, name, Pop ());
					}
					break;
				}
			case Opcode.LoadGlobal:
				{
					string name = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
					if (Top.Module.Attributes.ContainsKey (name)) {
						Push (Top.Module.GetAttribute (this, name));
					} else if (Globals.ContainsKey (name)) {
						Push (Globals [name]);
					} else {
						RaiseException (new IodineAttributeNotFoundException (name));
					}
					break;
				}
			case Opcode.StoreAttribute:
				{
					IodineObject target = Pop ();
					IodineObject value = Pop ();
					string attribute = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
					if (target.Attributes.ContainsKey (attribute) &&
					    target.Attributes [attribute] is IIodineProperty) {
						IIodineProperty property = (IIodineProperty)target.Attributes [attribute];
						property.Set (this, value);
						break;
					}
					target.SetAttribute (this, attribute, value);
					break;
				}
			case Opcode.LoadAttribute:
				{
					IodineObject target = Pop ();
					string attribute = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
					if (target.Attributes.ContainsKey (attribute) &&
					    target.Attributes [attribute] is IIodineProperty) {
						IIodineProperty property = (IIodineProperty)target.Attributes [attribute];
						Push (property.Get (this));
						break;
					}
					Push (target.GetAttribute (this, attribute));
					break;
				}
			case Opcode.LoadAttributeOrNull:
				{
					IodineObject target = Pop ();
					string attribute = ((IodineName)Top.Module.ConstantPool [instruction.Argument]).Value;
					if (target.Attributes.ContainsKey (attribute)) {
						Push (target.GetAttribute (this, attribute));
					} else {
						Push (IodineNull.Instance);
					}
					break;
				}
			case Opcode.StoreIndex:
				{
					IodineObject index = Pop ();
					IodineObject target = Pop ();
					IodineObject value = Pop ();
					target.SetIndex (this, index, value);
					break;
				}
			case Opcode.LoadIndex:
				{
					IodineObject index = Pop ();
					IodineObject target = Pop ();
					Push (target.GetIndex (this, index));
					break;
				}
			case Opcode.BinOp:
				{
					IodineObject op2 = Pop ();
					IodineObject op1 = Pop ();
					Push (op1.PerformBinaryOperation (this,
						(BinaryOperation)instruction.Argument,
						op2));
					break;
				}
			case Opcode.UnaryOp:
				{
					Push (Pop ().PerformUnaryOperation (this, 
						(UnaryOperation)instruction.Argument));
					break;
				}
			case Opcode.Invoke:
				{
					IodineObject target = Pop ();
					IodineObject[] arguments = new IodineObject[instruction.Argument];
					for (int i = 1; i <= instruction.Argument; i++) {
						arguments [instruction.Argument - i] = Pop ();
					}
					Push (target.Invoke (this, arguments));
					break;
				}
			case Opcode.InvokeVar:
				{
					IodineObject target = Pop ();
					List<IodineObject> arguments = new List<IodineObject> ();
					IodineTuple tuple = Pop () as IodineTuple;
					if (tuple == null) {
						RaiseException (new IodineTypeException ("Tuple"));
						break;
					}
					for (int i = 0; i < instruction.Argument; i++) {
						arguments.Add (Pop ());
					}
					arguments.AddRange (tuple.Objects);
					Push (target.Invoke (this, arguments.ToArray ()));
					break;
				}
			case Opcode.InvokeSuper:
				{
					IodineTypeDefinition target = (IodineTypeDefinition)Pop ();
					IodineObject[] arguments = new IodineObject[instruction.Argument];
					for (int i = 1; i <= instruction.Argument; i++) {
						arguments [instruction.Argument - i] = Pop ();
					}
					target.Inherit (this, Top.Self, arguments);
					break;
				}
			case Opcode.Return:
				{
					Top.InstructionPointer = int.MaxValue;
					break;
				}
			case Opcode.Yield:
				{
					Top.Yielded = true;
					break;
				}
			case Opcode.JumpIfTrue:
				{
					if (Pop ().IsTrue ()) {
						Top.InstructionPointer = instruction.Argument;
					}
					break;
				}
			case Opcode.JumpIfFalse:
				{
					if (!Pop ().IsTrue ()) {
						Top.InstructionPointer = instruction.Argument;
					}
					break;
				}
			case Opcode.Jump:
				{
					Top.InstructionPointer = instruction.Argument;
					break;
				}
			case Opcode.BuildHash:
				{
					IodineHashMap hash = new IodineHashMap ();
					for (int i = 0; i < instruction.Argument; i++) {
						IodineObject val = Pop ();
						IodineObject key = Pop ();
						hash.Set (key, val);
					}
					Push (hash);
					break;
				}
			case Opcode.BuildList:
				{
					IodineObject[] items = new IodineObject[instruction.Argument];
					for (int i = 1; i <= instruction.Argument; i++) {
						items [instruction.Argument - i] = Pop ();
					}
					Push (new IodineList (items));
					break;
				}
			case Opcode.BuildTuple:
				{
					IodineObject[] items = new IodineObject[instruction.Argument];
					for (int i = 1; i <= instruction.Argument; i++) {
						items [instruction.Argument - i] = Pop ();
					}
					Push (new IodineTuple (items));
					break;
				}
			case Opcode.BuildClosure:
				{
					IodineMethod method = Pop () as IodineMethod;
					Push (new IodineClosure (Top, method));
					break;
				}
			case Opcode.IterGetNext:
				{
					Push (Pop ().IterGetCurrent (this));
					break;
				}
			case Opcode.IterMoveNext:
				{
					Push (IodineBool.Create (Pop ().IterMoveNext (this)));
					break;
				}
			case Opcode.IterReset:
				{
					Pop ().IterReset (this);
					break;
				}
			case Opcode.PushExceptionHandler:
				{
					Top.ExceptionHandlers.Push (new IodineExceptionHandler (frameCount, instruction.Argument));
					break;
				}
			case Opcode.PopExceptionHandler:
				{
					Top.ExceptionHandlers.Pop ();
					break;
				}
			case Opcode.InstanceOf:
				{
					IodineObject o = Pop ();
					IodineTypeDefinition type = Pop () as IodineTypeDefinition;
					if (type == null) {
						RaiseException (new IodineTypeException ("TypeDef"));
						break;
					}
					Push (IodineBool.Create (o.InstanceOf (type)));
					break;
				}
			case Opcode.DynamicCast:
				{
					IodineObject o = Pop ();
					IodineTypeDefinition type = Pop () as IodineTypeDefinition;
					if (type == null) {
						RaiseException (new IodineTypeException ("TypeDef"));
						break;
					}
					if (o.InstanceOf (type)) {
						Push (o);
					} else {
						Push (IodineNull.Instance);
					}
					break;
				}
			case Opcode.NullCoalesce:
				{
					IodineObject o1 = Pop ();
					IodineObject o2 = Pop ();
					if (o1 is IodineNull) {
						Push (o2);
					} else {
						Push (o1);
					}
					break;
				}
			case Opcode.BeginExcept:
				{
					bool rethrow = true;
					for (int i = 1; i <= instruction.Argument; i++) {
						IodineTypeDefinition type = Pop () as IodineTypeDefinition;
						if (type == null) {
							RaiseException (new IodineTypeException ("TypeDef"));
							break;
						}

						if (lastException.InstanceOf (type)) {
							rethrow = false;
							break;
						}
					}
					if (rethrow) {
						RaiseException (lastException);
					}
					break;
				}
			case Opcode.Raise:
				{
					IodineObject e = Pop ();
					if (e.InstanceOf (IodineException.TypeDefinition)) {
						RaiseException (e);
					} else {
						RaiseException (new IodineTypeException ("Exception"));
					}
					break;
				}
			case Opcode.SwitchLookup:
				{
					Dictionary<int, IodineObject> lookup = new Dictionary<int, IodineObject> ();
					int needle = Pop ().GetHashCode ();
					for (int i = 0; i < instruction.Argument; i++) {
						IodineObject value = Pop ();
						IodineObject key = Pop ();
						lookup [key.GetHashCode ()] = value;
					}
					if (lookup.ContainsKey (needle)) {
						lookup [needle].Invoke (this, new IodineObject[] { });
						Push (IodineBool.True);
					} else {
						Push (IodineBool.False);
					}
					break;
				}
			case Opcode.BeginWith:
				{
					IodineObject obj = Pop ();
					obj.Enter (this);
					Top.DisposableObjects.Push (obj);
					break;
				}
			case Opcode.EndWith:
				{
					Top.DisposableObjects.Pop ().Exit (this);
					break;
				}
			}

		}
Ejemplo n.º 10
0
 public Error(ErrorType etype, SourceLocation location, string text)
 {
     ErrorType = etype;
     Text      = text;
     Location  = location;
 }
Ejemplo n.º 11
0
		private void Trace (TraceType type, StackFrame frame, SourceLocation location)
		{
			pauseVirtualMachine.WaitOne ();
			if (traceCallback (type, this, frame, location)) {
				pauseVirtualMachine.Reset ();
			}
		}
Ejemplo n.º 12
0
		private IodineObject Invoke (IodineMethod method, IodineObject[] arguments)
		{
			if (method.Body.Count > 0) {
				currentLocation = method.Body [0].Location;
			}

			int insCount = method.Body.Count;
			int prevStackSize = stackSize;
			int i = 0;

			foreach (string param in method.Parameters.Keys) {
				if (method.Variadic && (method.AcceptsKeywordArgs ? i == method.Parameters.Keys.Count - 2 :
					i == method.Parameters.Keys.Count - 1)) {
					IodineObject[] tupleItems = new IodineObject[arguments.Length - i];
					Array.Copy (arguments, i, tupleItems, 0, arguments.Length - i);
					Top.StoreLocal (method.Parameters [param], new IodineTuple (tupleItems));
				} else if (i == method.Parameters.Keys.Count - 1 && method.AcceptsKeywordArgs) {
					if (i < arguments.Length && arguments [i] is IodineHashMap) {
						Top.StoreLocal (method.Parameters [param], arguments [i]);
					} else {
						Top.StoreLocal (method.Parameters [param], new IodineHashMap ());
					}
				} else {
					Top.StoreLocal (method.Parameters [param], arguments [i++]);
				}
			}
			StackFrame top = Top;

			if (traceCallback != null) {
				Trace (TraceType.Function, top, currentLocation);
			}

			while (top.InstructionPointer < insCount && !top.AbortExecution && !Top.Yielded) {
				instruction = method.Body [Top.InstructionPointer++];
				if (traceCallback != null && instruction.Location.Line != currentLocation.Line) {
					Trace (TraceType.Line, top, instruction.Location);
				}
				ExecuteInstruction ();
				top.Location = currentLocation;
			}

			IodineObject retVal = lastObject ?? IodineNull.Instance;

			while (top.DisposableObjects.Count > 0) {
				top.DisposableObjects.Pop ().Exit (this);
			}

			stackSize = prevStackSize;

			if (top.AbortExecution) {
				return IodineNull.Instance;
			}

			EndFrame ();

			return retVal;
		}
Ejemplo n.º 13
0
 public void EmitInstruction(SourceLocation loc, Opcode opcode, Label label)
 {
     labelReferences [instructions.Count] = label;
     instructions.Add(new Instruction(loc, opcode, 0));
 }
Ejemplo n.º 14
0
		public void EmitInstruction (SourceLocation loc, Opcode opcode, IodineLabel label)
		{
			labelReferences [instructions.Count] = label;
			instructions.Add (new Instruction (loc, opcode, 0));
		}
Ejemplo n.º 15
0
		private int ReadChar ()
		{
			if (position >= sourceLen) {
				return -1;
			}

			if (source [position] == '\n') {
				location = new SourceLocation (location.Line + 1, 0, this.file); 
			} else {
				location = new SourceLocation (location.Line, location.Column + 1,
					this.file); 
			}
			return source [position++];
		}
Ejemplo n.º 16
0
        private static AstNode ParseString(SourceLocation loc, string str)
        {
            int pos = 0;
            string accum = "";
            List<string> vars = new List<string> ();
            while (pos < str.Length) {
                if (str [pos] == '#' && str.Length != pos + 1 && str [pos + 1] == '{') {
                    string substr = str.Substring (pos + 2);
                    if (substr.IndexOf ('}') == -1)
                        return null;
                    substr = substr.Substring (0, substr.IndexOf ('}'));
                    pos += substr.Length + 3;
                    vars.Add (substr);
                    accum += "{}";

                } else {
                    accum += str [pos++];
                }
            }
            StringExpression ret = new StringExpression (loc, accum);

            foreach (string name in vars) {
                ret.Add (new NameExpression (loc, name));
            }
            return ret;
        }
Ejemplo n.º 17
0
 public void EmitInstruction(SourceLocation loc, Opcode opcode)
 {
     instructions.Add(new Instruction(loc, opcode));
 }