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
The start of functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
dialupnoises committed Jun 11, 2016
1 parent 86539b2 commit bceae65
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
10 changes: 10 additions & 0 deletions Rant/Internal/VM/Compiler/BytecodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ public void AddGeneric(RantOpCode opcode, IEnumerable<byte> data)
_lastWasString = false;
}

/// <summary>
/// Add an opcode without any special treatment.
/// </summary>
/// <param name="opcode">The opcode to add.</param>
public void AddGeneric(RantOpCode opcode)
{
_bytecode.Add((byte)opcode);
_lastWasString = false;
}

/// <summary>
/// Add an opcode that references a string in the string table.
/// </summary>
Expand Down
51 changes: 47 additions & 4 deletions Rant/Internal/VM/Compiler/Parselets/FunctionParselet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using Rant.Internal.VM.Instructions;
using Rant.Internal.Stringes;

namespace Rant.Internal.VM.Compiler.Parselets
{
Expand All @@ -12,17 +13,59 @@ internal class FunctionParselet : IParselet
public IEnumerator<IParselet> Parse(TokenReader reader, BytecodeGenerator generator)
{
var name = reader.Read(R.Text, "Function name").Value;
// read arguments
while(!reader.End)
int argumentCount = 0;
if(reader.PeekToken().ID == R.Colon)
{
generator.LatestSegment.AddGeneric(RantOpCode.NewOutput)

reader.ReadLooseToken();
if(reader.PeekLooseToken().ID == R.RightSquare)
{
throw new RantCompilerException(reader.SourceName, reader.PeekLooseToken(), "Expected arguments, got end-of-function.");
}

// read arguments
while(!reader.End)
{
generator.LatestSegment.AddGeneric(RantOpCode.NewOutput);
yield return new PatternParselet(true);
generator.LatestSegment.AddGeneric(RantOpCode.ReturnOutput);
argumentCount++;

if(reader.PeekLooseToken().ID == R.RightSquare)
{
break;
}
reader.ReadLooseToken();
}

if(reader.End)
{
throw new RantCompilerException(reader.SourceName, reader.PrevToken, "Unexpected end-of-pattern.");
}
}

switch(name)
{
// optimize some special cases
case "close":
CheckArgumentCount(reader, name, 0, argumentCount);
generator.LatestSegment.AddGeneric(RantOpCode.CloseChannelConstant);
break;
default:
// TODO: add real native functions.
generator.LatestSegment.AddGeneric(RantOpCode.NativeCall, BitConverter.GetBytes((short)0));
break;
}
reader.Read(R.RightSquare, "End of function");
yield break;
}

private void CheckArgumentCount(TokenReader reader, Stringes.Stringe name, int expected, int actual)
{
if(expected != actual)
throw new RantCompilerException(
reader.SourceName,
name,
"Error: invalid number of arguments for " + name.Value + ". Got " + actual + ", expected " + expected + ".");
}
}
}
1 change: 0 additions & 1 deletion Rant/Internal/VM/Compiler/Parselets/PatternParselet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public IEnumerator<IParselet> Parse(TokenReader reader, BytecodeGenerator genera

if(_isFunctionArgument && (token.ID == R.Semicolon || token.ID == R.RightSquare))
{
reader.ReadToken();
yield break;
}

Expand Down
3 changes: 2 additions & 1 deletion Rant/Internal/VM/RantObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ public void Compare(RantObject b, out int result, out bool comparable)
const int greater = 1;
const int less = -1;
const int mismatch = 2;
comparable = true;
result = mismatch;
comparable = true;
switch (Type)
{
case RantObjectType.Number:
Expand Down
24 changes: 14 additions & 10 deletions Rant/Rant.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,19 @@
<ItemGroup>
<Compile Include="Internal\Engine\Compiler\DefaultParseletAttribute.cs" />
<Compile Include="Internal\Engine\Compiler\DefaultParserAttribute.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\BlockParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\BlockWeightParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\FunctionRegexParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\FunctionSubroutineParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\FunctionTextParselet.cs" />
<Compile Include="Internal\Engine\Compiler\RantCompiler.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\ConstantLiteralParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\DefaultParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\EscapeSequenceParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\QueryParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\BlockParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\FunctionParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\FunctionRegexParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\FunctionSubroutineParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\FunctionTextParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\Parselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\QueryParselet.cs" />
<Compile Include="Internal\Engine\Compiler\Parselets\WhitespaceParselet.cs" />
<Compile Include="Internal\Engine\Compiler\RantCompiler.cs" />
<Compile Include="Internal\Engine\Compiler\RantExpressionCompiler.cs" />
<Compile Include="Internal\Engine\Compiler\TokenParserAttribute.cs" />
<Compile Include="Internal\Engine\Constructs\AttribPersistence.cs" />
Expand All @@ -235,7 +235,6 @@
<Compile Include="Internal\Engine\Output\OutputChainArticleBuffer.cs" />
<Compile Include="Internal\Engine\Output\OutputChainBuffer.cs" />
<Compile Include="Internal\Engine\Output\OutputWriter.cs" />
<Compile Include="Internal\VM\Compiler\Parselets\ArgumentableParselet.cs" />
<Compile Include="Internal\VM\Compiler\Parselets\FunctionParselet.cs" />
<Compile Include="Internal\VM\Compiler\Parselets\TagParselet.cs" />
<Compile Include="Internal\VM\Output\OutputChain.cs" />
Expand Down Expand Up @@ -331,16 +330,21 @@
<Compile Include="Internal\IO\Compression\RangeCoder\RangeCoder.cs" />
<Compile Include="Internal\IO\Compression\RangeCoder\RangeCoderBit.cs" />
<Compile Include="Internal\IO\Compression\RangeCoder\RangeCoderBitTree.cs" />
<Compile Include="Internal\VM\BlockData.cs" />
<Compile Include="Internal\VM\CallFrame.cs" />
<Compile Include="Internal\VM\Compiler\BytecodeGenerator.cs" />
<Compile Include="Internal\VM\Compiler\Parselets\EscapeParselet.cs" />
<Compile Include="Internal\VM\Compiler\Parselets\IParselet.cs" />
<Compile Include="Internal\VM\Compiler\Parselets\PatternParselet.cs" />
<Compile Include="Internal\VM\Compiler\R.cs" />
<Compile Include="Internal\VM\Compiler\RantCompiler.cs" />
<Compile Include="Internal\VM\Compiler\RantLexer.cs" />
<Compile Include="Internal\VM\Compiler\TokenParserAttribute.cs" />
<Compile Include="Internal\VM\Compiler\TokenReader.cs" />
<Compile Include="Internal\VM\Instructions\RantOpCode.cs" />
<Compile Include="Internal\VM\BlockData.cs" />
<Compile Include="Internal\VM\CallFrame.cs" />
<Compile Include="Internal\VM\RantProgramData.cs" />
<Compile Include="Internal\VM\RantObject.cs" />
<Compile Include="Internal\VM\RantObjectType.cs" />
<Compile Include="Internal\VM\RantProgramData.cs" />
<Compile Include="Internal\VM\RVM.cs" />
<Compile Include="RantArgAttribute.cs" />
<Compile Include="RantCompilerException.cs" />
Expand Down

0 comments on commit bceae65

Please sign in to comment.