Skip to content

Commit

Permalink
Wire compiler actions to the grammar engine
Browse files Browse the repository at this point in the history
This wiring takes place at a lower level than just creating a p6-level
actions class.  This might count as premature optimization.
  • Loading branch information
sorear committed Jun 24, 2012
1 parent dda9f42 commit bea180f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
51 changes: 50 additions & 1 deletion lib/Actions.cs
Expand Up @@ -8,8 +8,57 @@
namespace Niecza.Compiler {
class Actions {
internal CompJob job;

RxOp.RxOp rnoop = new RxOp.Sequence(new RxOp.RxOp[0]);
public Actions(CompJob job) { this.job = job; }

Dictionary<string,Action<Cursor>> acts;

public Actions(CompJob job) {
this.job = job;
acts = new Dictionary<string,Action<Cursor>>();

Type tt = typeof(Actions);
foreach (var m in tt.GetMethods()) {
if (m.DeclaringType != tt || !m.IsPublic)
continue;
acts[m.Name] = (Action<Cursor>) Delegate.CreateDelegate(
typeof(Action<Cursor>), this, m);
}
}

internal void CallAction(Frame th, string name, Cursor m) {
Action<Cursor> act;
string cooked_name = name;
int ix = name.IndexOf(':');
if (ix >= 0) {
var sb = new StringBuilder();
sb.Append(name.Substring(0,ix));
int lix = name.Length;
if (lix - ix >= 6 && name.Substring(ix+1,3) == "sym") {
lix --; ix += 5; // :sym<
}
sb.Append("__");
for (int i = ix; i < lix; i++) {
char c = name[i];
if (c == '_' || (c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
sb.Append(c);
} else {
sb.AppendFormat("{0:x2}", (int)c);
}
}
cooked_name = sb.ToString();
}
acts.TryGetValue(cooked_name, out act);
if (Cursor.Trace)
Console.WriteLine("Call action {0} - {1}", cooked_name,
act != null ? "found" : "not found");
if (act != null) {
job.topframe = th;
job.curlex = Kernel.UnboxAny<SubInfo>(job.context("$*CURLEX").Fetch());
act(m);
}
}

// Little things to make writing wads of data-extraction code nicer...
string asstr(Variable v) { return v.Fetch().mo.mro_raw_Str.Get(v); }
Expand Down
8 changes: 8 additions & 0 deletions lib/CompMgr.cs
Expand Up @@ -73,7 +73,11 @@ public struct RxInfo {

// state vars
public RuntimeUnit unit; // $*unit

// this one is used SO MUCH it gets to stay :>
public SubInfo curlex; // $*CURLEX
public Frame topframe;

public RxInfo rxinfo; // %*RX
public Actions actions; // $*ACTIONS
public string in_decl; // $*IN_DECL
Expand All @@ -88,6 +92,10 @@ public struct RxInfo {
public bool monkey_typing;
public bool catchy;

public Variable context(string name) {
return Kernel.ContextHelper(topframe, name, 0);
}

public Variable get_memo(int pos, string key) { throw new NotImplementedException(); } //SSTO

public Dictionary<string,string> proto_endsym =
Expand Down
8 changes: 7 additions & 1 deletion lib/Cursor.cs
Expand Up @@ -12,9 +12,15 @@ public sealed class GState {
public int highwater;

public Variable actions;
internal Niecza.Compiler.Actions comp;

internal Frame CallAction(Frame th, string name, Cursor match) {
if (actions == null || name == "" || name == null)
if (name == "" || name == null)
return th;

if (comp != null)
comp.CallAction(th, name, match);
if (actions == null)
return th;
if (Cursor.Trace)
Console.WriteLine("To call action {0}", name);
Expand Down

0 comments on commit bea180f

Please sign in to comment.