-
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
Merged
Crazycolorz5
merged 8 commits into
FireEmblemUniverse:master
from
StanHash:builtin-macros
Mar 21, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
708b837
(builtin-macros) IsDefined
StanHash 2462c96
(builtin-macros) non-singleton String for consistency
StanHash 7a2f5b4
(builtin-macros) AddToPool and pool
StanHash c7dc132
(builtin-macros) move pool label generated to AddToPool
StanHash 90ffd4b
(builtin-macros) #pool expands to BlockNode instead of tokens
StanHash 8786463
(builtin-macros) proper errors on unpooled data
StanHash 2fd3e56
(builtin-macros) fix scope issues with AddToPool/pool
StanHash 22784a7
(builtin-macros) fix nested AddToPool
StanHash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using ColorzCore.DataTypes; | ||
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, ImmutableStack<Closure> scopes) | ||
{ | ||
List<Token> line = new List<Token>(6 + parameters[0].Count); | ||
|
||
string labelName = ParentParser.Pool.MakePoolLabelName(); | ||
|
||
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.Pool.Lines.Add(new Pool.PooledLine(scopes, line)); | ||
|
||
yield return new Token(TokenType.IDENTIFIER, head.Location, labelName); | ||
} | ||
|
||
public override bool ValidNumParams(int num) | ||
{ | ||
return num == 1 || num == 2; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using ColorzCore.DataTypes; | ||
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, ImmutableStack<Closure> scopes) | ||
{ | ||
if (parameters[0].Count != 1) | ||
{ | ||
// TODO: err somehow | ||
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using ColorzCore.DataTypes; | ||
using ColorzCore.Lexer; | ||
|
||
namespace ColorzCore.Parser | ||
{ | ||
class Pool | ||
{ | ||
public struct PooledLine | ||
{ | ||
public ImmutableStack<Closure> Scope { get; private set; } | ||
public List<Token> Tokens { get; private set; } | ||
|
||
public PooledLine(ImmutableStack<Closure> scope, List<Token> tokens) | ||
{ | ||
Scope = scope; | ||
Tokens = tokens; | ||
} | ||
} | ||
|
||
public static readonly string pooledLabelPrefix = "__POOLED$"; | ||
|
||
public List<PooledLine> Lines { get; private set; } | ||
|
||
private long poolLabelCounter; | ||
|
||
public Pool() | ||
{ | ||
Lines = new List<PooledLine>(); | ||
poolLabelCounter = 0; | ||
} | ||
|
||
public string MakePoolLabelName() | ||
{ | ||
// The presence of $ in the label name guarantees that it can't be a user label | ||
return string.Format("{0}{1}", pooledLabelPrefix, poolLabelCounter++); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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)