It's kind of the house that jack built. Currently to render my shared codebase (such as table tokenizing support) in a language independent manner I have to render the entire thing using the codedom, as expression trees won't give me source code, and can't define whole classes.
Here's what I'm doing now:
var result = CD.Struct("Token", false,
CD.Field(typeof(int), "Line", MemberAttributes.Public),
CD.Field(typeof(int), "Column", MemberAttributes.Public),
CD.Field(typeof(long), "Position", MemberAttributes.Public),
CD.Field(typeof(int), "SymbolId", MemberAttributes.Public),
CD.Field(typeof(string), "Value", MemberAttributes.Public));
result.CustomAttributes.Add(_GeneratedCodeAttribute);
return result;
in order to declare this:
public struct Token
{
public int Line;
public int Column;
public long Position;
public int SymbolId;
public string Value;
}
And never mind methods like this:
result.Statements.AddRange(new CodeStatement[] {
CD.If(CD.Invoke(input,"MoveNext"),
CD.IfElse(CD.NotEq(state,CD.Literal(_BeforeBegin)), new CodeStatement[] {
CD.Let(position,CD.Add(position,CD.One)),
CD.IfElse(CD.Eq(CD.Literal('\n'),current),new CodeStatement[] {
CD.Let(column,CD.One),
CD.Let(line,CD.Add(line,CD.One))
},
CD.IfElse(CD.Eq(CD.Literal('\t'),current),new CodeStatement[]
{
CD.Let(column,CD.Add(column,CD.Literal(_TabWidth)))
},
CD.Let(column,CD.Add(column,CD.One))))
},
CD.IfElse(CD.Eq(CD.Literal('\n'),current),new CodeStatement[] {
CD.Let(column,CD.One),
CD.Let(line,CD.Add(line,CD.One))
},
CD.If(CD.Eq(CD.Literal('\t'),current),
CD.Let(column,CD.Add(column,CD.Literal(_TabWidth-1))))
)
),
CD.Return(CD.True)),
CD.Let(state,CD.Literal(_InnerFinished)),
CD.Return(CD.False)
});
return result;
to declare this:
bool _MoveNextInput()
{
if (_input.MoveNext())
{
if (_BeforeBegin != _state)
{
++_position;
if ('\n' == _input.Current)
{
_column = 1;
++_line;
}
else if ('\t' == _input.Current)
_column += _TabWidth;
else
++_column;
} else
{
if ('\n' == _input.Current)
{
_column = 1;
++_line;
}
else if ('\t' == _input.Current)
_column += _TabWidth-1;
}
return true;
}
_state = _InnerFinished;
return false;
}
It's ridiculous, and I'd like to move away from it toward something more natural.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|