-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add IsDefined builtin and AddToPool/#pool #40
Changes from 3 commits
708b837
2462c96
7a2f5b4
c7dc132
90ffd4b
8786463
2fd3e56
22784a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using ColorzCore.Lexer; | ||
|
||
namespace ColorzCore.Parser.Macros | ||
{ | ||
class AddToPool : BuiltInMacro | ||
{ | ||
/* | ||
* Macro Usage: | ||
* AddToPool(tokens...): adds token to pool, and expands to label name referring to those tokens | ||
* AddToPool(tokens..., alignment): adds token to pool and make sure pooled tokens are aligned given alignment | ||
*/ | ||
|
||
public EAParser ParentParser { get; private set; } | ||
|
||
public AddToPool(EAParser parent) | ||
{ | ||
ParentParser = parent; | ||
} | ||
|
||
public override IEnumerable<Token> ApplyMacro(Token head, IList<IList<Token>> parameters) | ||
{ | ||
List<Token> line = new List<Token>(6 + parameters[0].Count); | ||
|
||
string labelName = ParentParser.MakePoolLabelName(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To not overload the role of EAParser doing everything (which it is dangerously close to), let's try to have this as a static method here. |
||
|
||
if (parameters.Count == 2) | ||
{ | ||
// Add Alignment directive | ||
|
||
line.Add(new Token(TokenType.IDENTIFIER, head.Location, "ALIGN")); | ||
line.Add(parameters[1][0]); | ||
line.Add(new Token(TokenType.SEMICOLON, head.Location, ";")); | ||
} | ||
|
||
// TODO: Make label declaration global (when this feature gets implemented) | ||
// This way the name will be available as long as it is pooled (reguardless of pool scope) | ||
|
||
line.Add(new Token(TokenType.IDENTIFIER, head.Location, labelName)); | ||
line.Add(new Token(TokenType.COLON, head.Location, ":")); | ||
|
||
line.AddRange(parameters[0]); | ||
line.Add(new Token(TokenType.NEWLINE, head.Location, "\n")); | ||
|
||
ParentParser.PooledLines.Add(line); | ||
|
||
yield return new Token(TokenType.IDENTIFIER, head.Location, labelName); | ||
} | ||
|
||
public override bool ValidNumParams(int num) | ||
{ | ||
return num == 1 || num == 2; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using ColorzCore.Lexer; | ||
|
||
namespace ColorzCore.Parser.Macros | ||
{ | ||
class IsDefined : BuiltInMacro | ||
{ | ||
public EAParser ParentParser { get; private set; } | ||
|
||
public IsDefined(EAParser parent) | ||
{ | ||
ParentParser = parent; | ||
} | ||
|
||
public override IEnumerable<Token> ApplyMacro(Token head, IList<IList<Token>> parameters) | ||
{ | ||
if (parameters[0].Count != 1) | ||
{ | ||
// TODO: err somehow | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably do some param checking in the invocation of built in macros akin to how it is for preproc defs. (you don't need to address this comment if you don't want to; I intend to) |
||
yield return MakeFalseToken(head.Location); | ||
} | ||
else | ||
{ | ||
Token token = parameters[0][0]; | ||
|
||
if ((token.Type == TokenType.IDENTIFIER) && IsReallyDefined(token.Content)) | ||
{ | ||
yield return MakeTrueToken(head.Location); | ||
} | ||
else | ||
{ | ||
yield return MakeFalseToken(head.Location); | ||
} | ||
} | ||
} | ||
|
||
public override bool ValidNumParams(int num) | ||
{ | ||
return num == 1; | ||
} | ||
|
||
protected bool IsReallyDefined(string name) | ||
{ | ||
return ParentParser.Definitions.ContainsKey(name) || ParentParser.Macros.ContainsName(name); | ||
Crazycolorz5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
protected static Token MakeTrueToken(DataTypes.Location location) | ||
{ | ||
return new Token(TokenType.NUMBER, location, "1"); | ||
} | ||
|
||
protected static Token MakeFalseToken(DataTypes.Location location) | ||
{ | ||
return new Token(TokenType.NUMBER, location, "0"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using ColorzCore.DataTypes; | ||
using ColorzCore.Lexer; | ||
using ColorzCore.Parser; | ||
using ColorzCore.Parser.AST; | ||
|
||
namespace ColorzCore.Preprocessor.Directives | ||
{ | ||
class PoolDirective : IDirective | ||
{ | ||
public int MinParams => 0; | ||
public int? MaxParams => 0; | ||
public bool RequireInclusion => true; | ||
|
||
public Maybe<ILineNode> Execute(EAParser p, Token self, IList<IParamNode> parameters, MergeableGenerator<Token> tokens) | ||
{ | ||
foreach (List<Token> line in p.PooledLines) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will write the pooled lines in reverse order. Since order isn't meant to matter, it's probably fine, but I want to make sure it's intensional. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting quirk here, since the tokens are tagged with their original location, "relocating" them via pool shouldn't affect error parsing. I think. Please test error messages to confirm they error on the AddToPool() and not on the #pool. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error messages are indeed being linked to the AddToPool invocation (and not the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would probably be extraneous/difficult to implement. (hm, if I were to implement it, I'd probably do some kind of algebraic effects on the error handler to wrap adding a "pooled at" message to the default handler... within the pool directive scope. but endsidethought) |
||
{ | ||
tokens.PrependEnumerator(line.GetEnumerator()); | ||
} | ||
|
||
p.PooledLines.Clear(); | ||
|
||
return new Nothing<ILineNode>(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error or warning upon completion of parsing if the pool is not emptied at the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, thoughts on associating pooled lines to scope, and thus erroring if there are still pooled lines at the end of a scope?
Or do you think pooled lines should be able to escape to outer scopes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this. Putting extra data somewhere without the user explicitly asking for it sounds like a bad idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you misunderstand me. I mean if I have
Should this error? Arguably, pooled data should be required only within its scope, so lack of a #pool in the scope may be an error, so we may want to enforce pooling on a scope level... was my thought
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I think pooling outside of scope that way is fine, I think it is only problematic if the
#pool
were to be inside a child scope and not parent scope... But even then maybe it isn't idk(but yes I did misread/read too quickly and thought doing silent stuff instead of err sorry).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait I think I see a (technical) issue now: you wouldn't be able to access scope local labels referenced within the AddToPool expression if the corresponding
#pool
was outside... ughThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's associate pooled lines with the scope they're used in, then, and error at the end of a scope if there are undumped pooled lines.