Skip to content

Commit

Permalink
Emitters Base class
Browse files Browse the repository at this point in the history
  • Loading branch information
vinaychandra committed Sep 4, 2017
1 parent dc13d8e commit 63cf0d4
Show file tree
Hide file tree
Showing 5 changed files with 417 additions and 393 deletions.
68 changes: 40 additions & 28 deletions source/Spruce/Tokens/Root.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,38 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using Spruce;

namespace Spruce.Tokens {
public class Root : Token {
public Root(object aEmitter = null) : base() {
if (aEmitter == null) {
return;
}

namespace Spruce.Tokens
{
public class Root : Token
{
public void AddEmitter(object aEmitter)
{
// Load emitters to pattern list
foreach (var xMethod in aEmitter.GetType().GetRuntimeMethods()) {
foreach (var xAttrib in xMethod.GetCustomAttributes<Attribs.Emitter>()) {
foreach (var xMethod in aEmitter.GetType().GetRuntimeMethods())
{
foreach (var xAttrib in xMethod.GetCustomAttributes<Attribs.Emitter>())
{
Token.Action xAction;
var xParams = xMethod.GetParameters();
if (xParams.Length == 0) {
xAction = (List<CodePoint> aPoints) => {
xMethod.Invoke(aEmitter, null);
};
} else {
if (xParams.Length == 0)
{
xAction = (List<CodePoint> aPoints) => { xMethod.Invoke(aEmitter, null); };
}
else
{
var xParam = xParams[0];
if (xParam.ParameterType.IsGenericType) {
xAction = (List<CodePoint> aPoints) => {
if (xParam.ParameterType.IsGenericType)
{
xAction = (List<CodePoint> aPoints) =>
{
xMethod.Invoke(aEmitter, new object[] { aPoints });
};
} else {
xAction = (List<CodePoint> aPoints) => {
}
else
{
xAction = (List<CodePoint> aPoints) =>
{
xMethod.Invoke(aEmitter, aPoints.Select(q => q.Value).ToArray());
};
}
Expand All @@ -39,32 +43,40 @@ public Root(object aEmitter = null) : base() {
}
}

public override object Parse(string aText, ref int rStart) {
public override object Parse(string aText, ref int rStart)
{
throw new Exception("Parse not valid on Root.");
}

public List<CodePoint> Parse(string aText) {
try {
public List<CodePoint> Parse(string aText)
{
try
{
// Important for end detection. Do not TrimStart, will goof up CodePoint indexes.
aText = aText.TrimEnd();
var xResult = new List<CodePoint>();
int aPos = 0;
Token xToken = this;

while (aPos < aText.Length) {
while (aPos < aText.Length)
{
var xCP = xToken.Next(aText, ref aPos);
if (xCP == null) {
if (xCP == null)
{
break;
}
xResult.Add(xCP);
xToken = xCP.Token;
}
if (aPos < aText.Length) {
if (aPos < aText.Length)
{
throw new Exception("Text on line beyond end of parse.");
}

return xResult;
} catch (Exception ex) {
}
catch (Exception ex)
{
throw new Exception("Parse Error: " + aText, ex);
}
}
Expand Down
9 changes: 4 additions & 5 deletions source/XSharp/Compiler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using XSharp.x86.Assemblers;

namespace XSharp
Expand All @@ -22,8 +19,10 @@ public Compiler(TextWriter aOut)
{
Out = aOut;
mNASM = new NASM(aOut);
var xEmitters = new Emitters.Emitters(this, mNASM);
mTokenMap = new Spruce.Tokens.Root(xEmitters);

mTokenMap = new Spruce.Tokens.Root();
mTokenMap.AddEmitter(new Emitters.Comments(this, mNASM));
mTokenMap.AddEmitter(new Emitters.AllEmitters(this, mNASM));
}

public void WriteLine(string aText = "")
Expand Down
Loading

0 comments on commit 63cf0d4

Please sign in to comment.