Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
/ rant3 Public archive

Commit

Permalink
Object model related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Fleck committed Jun 11, 2016
1 parent b810097 commit 72886d5
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 118 deletions.
6 changes: 5 additions & 1 deletion Rant/Internal/VM/CallFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ internal class CallFrame
// TODO: Query args
public string QueryTableName = String.Empty;
public int Position = 0;


public CallFrame(int position)
{
Position = position;
}
}
}
1 change: 0 additions & 1 deletion Rant/Internal/VM/Compiler/BytecodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Rant.Internal.VM.Instructions;

Expand Down
3 changes: 0 additions & 3 deletions Rant/Internal/VM/Compiler/Parselets/EscapeParselet.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Rant.Internal.VM.Instructions;

namespace Rant.Internal.VM.Compiler.Parselets
Expand Down
7 changes: 2 additions & 5 deletions Rant/Internal/VM/Compiler/Parselets/PatternParselet.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

using Rant.Internal.VM.Instructions;

namespace Rant.Internal.VM.Compiler.Parselets
Expand Down
18 changes: 14 additions & 4 deletions Rant/Internal/VM/Instructions/RantOpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ internal enum RantOpCode : byte
/// </summary>
PrintChars = 0x08,
/// <summary>
/// Calls the specified function. Arguments should be pushed to the stack.
/// <para>call [short func_code]</para>
/// Calls the specified native function. Arguments should be pushed to the stack.
/// <para>ncall [short func_code]</para>
/// </summary>
Call = 0x09,
NativeCall = 0x09,
/// <summary>
/// Returns to caller.
/// <para>ret</para>
Expand Down Expand Up @@ -91,7 +91,7 @@ internal enum RantOpCode : byte
PopSubs = 0x10,
/// <summary>
/// Calls the specified subroutine.
/// <para>callsub [int sub_id]</para>
/// <para>callsub [int sub_id] [int max_locals]</para>
/// </summary>
CallSub = 0x11,
/// <summary>
Expand Down Expand Up @@ -228,5 +228,15 @@ internal enum RantOpCode : byte
/// Pushes one to the stack.
/// </summary>
One = 0x2c,
/// <summary>
/// Pushes an address to the stack.
/// <para>pushaddr [int address]</para>
/// </summary>
PushAddress = 0x2d,
/// <summary>
/// Calls a Richard function.
/// <para>call [int func_location]</para>
/// </summary>
Call,
}
}
55 changes: 53 additions & 2 deletions Rant/Internal/VM/RVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ internal unsafe class RVM
public readonly Limit SizeLimit;
public RantFormat Format;

private readonly HashSet<CallFrame> _frameCache = new HashSet<CallFrame>();

private readonly RantProgram _program;
private readonly long _timeout = 0;
private readonly Stack<CallFrame> _callStack = new Stack<CallFrame>();
private readonly Stack<RantObject> _locals = new Stack<RantObject>(4);
private readonly Stack<RantObject> _stack = new Stack<RantObject>(4);
private readonly Stack<RantObject[]> _locals = new Stack<RantObject[]>();
private readonly Stack<RantObject> _args = new Stack<RantObject>();
private readonly Stack<OutputWriter> _writers = new Stack<OutputWriter>();

Expand Down Expand Up @@ -71,6 +74,8 @@ public RantOutput Run()
RantOpCode* ptrCodeOps;
byte* ptrCodeBytes;
RantObject roa, rob;
int cmp = 0;
bool cmpValid = false;

try
{
Expand Down Expand Up @@ -109,11 +114,57 @@ public RantOutput Run()
_writers.Peek().CloseChannel();
break;
case RantOpCode.Print:
Print(_locals.Pop());
Print(_stack.Pop());
break;
case RantOpCode.Return:
_callStack.Pop();
break;
case RantOpCode.PushString:
_stack.Push(new RantObject(_program.Data.GetString(*((int*)&ptrCodeBytes[frame.Position]))));
frame.Position += sizeof(int);
break;
case RantOpCode.PushNum:
_stack.Push(new RantObject(*((double*)&ptrCodeBytes[frame.Position])));
frame.Position += sizeof(double);
break;
case RantOpCode.Concat:
rob = _stack.Pop();
roa = _stack.Pop();
_stack.Push(new RantObject(String.Concat(roa, rob)));
break;
case RantOpCode.Add:
rob = _stack.Pop();
roa = _stack.Pop();
_stack.Push(roa + rob);
break;
case RantOpCode.Subtract:
rob = _stack.Pop();
roa = _stack.Pop();
_stack.Push(roa - rob);
break;
case RantOpCode.Multiply:
rob = _stack.Pop();
roa = _stack.Pop();
_stack.Push(roa * rob);
break;
case RantOpCode.Divide:
rob = _stack.Pop();
roa = _stack.Pop();
_stack.Push(roa / rob);
break;
case RantOpCode.Modulo:
rob = _stack.Pop();
roa = _stack.Pop();
_stack.Push(roa % rob);
break;
case RantOpCode.Jump:
frame.Position = *((int*)&ptrCodeBytes[frame.Position]);
continue;
case RantOpCode.Compare:
rob = _stack.Pop();
roa = _stack.Pop();
roa.Compare(rob, out cmp, out cmpValid);
break;
}
frame.Position++;
}
Expand Down

0 comments on commit 72886d5

Please sign in to comment.