Compare commits

...

5 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 07122472b7 Resolver
- Added `collectWithin(...)` and `collectUpwards(...)`
2024-05-08 12:57:53 +02:00
Tristan B. Velloza Kildaire b326eeba59 Test cases
- Added new `simple_aliases.t`
2024-05-08 09:33:01 +02:00
Tristan B. Velloza Kildaire 63ea709362 Check
- Added `ALIAS` as a new member to the `SymbolType` enum
- Added forward-mapping from `"alias"` to `ALIAS`
-
2024-05-08 09:32:47 +02:00
Tristan B. Velloza Kildaire a292809143 Parser
- Added `parseAliasDeclaration()`
- Both `parse()` and `parseStatement(...)` now support alias declarations
2024-05-08 09:32:12 +02:00
Tristan B. Velloza Kildaire 4f9ac9eb56 AliasDeclaration
- Added new type
2024-05-08 09:30:11 +02:00
5 changed files with 147 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import tlang.compiler.parsing.exceptions;
import tlang.compiler.core : Compiler;
import std.string : format;
import tlang.compiler.modman;
import tlang.compiler.symbols.aliases;
// TODO: Technically we could make a core parser etc
public final class Parser
@ -2324,6 +2325,43 @@ public final class Parser
}
}
/**
* Parses an alias declaration
*
* Returns: an `AliasDeclaration`
*/
private AliasDeclaration parseAliasDeclaration()
{
WARN("parseAliasDeclaration(): Enter");
AliasDeclaration aliasDecl;
/* Pop off the `alias` */
lexer.nextToken();
/* Consume the alias's name */
Token tok = lexer.getCurrentToken();
expect(SymbolType.IDENT_TYPE, tok);
string aliasName = tok.getToken();
/* Next token, expect `=` */
lexer.nextToken();
expect(SymbolType.ASSIGN, lexer.getCurrentToken());
/* Now consume an expression */
lexer.nextToken();
Expression aliasExpr = parseExpression();
expect(SymbolType.SEMICOLON, lexer.getCurrentToken());
lexer.nextToken();
/* Construct an alias with the name and expression */
aliasDecl = new AliasDeclaration(aliasName, aliasExpr);
WARN("parseAliasDeclaration(): Leave");
return aliasDecl;
}
// TODO: We need to add `parseComment()`
// support here (see issue #84)
// TODO: This ic currently dead code and ought to be used/implemented
@ -2405,6 +2443,11 @@ public final class Parser
ERROR("COMMENTS NOT YET PROPERLY SUPOORTED");
parseComment();
}
/* If it is an alias declaration */
else if(symbol == SymbolType.ALIAS)
{
statement = parseAliasDeclaration();
}
/* Error out */
else
{
@ -2794,6 +2837,11 @@ public final class Parser
ERROR("COMMENTS NOT YET PROPERLY SUPOORTED");
parseComment();
}
/* If it is an alias declaration */
else if(symbol == SymbolType.ALIAS)
{
modulle.addStatement(parseAliasDeclaration());
}
else
{
expect("parse(): Unknown '" ~ tok.getToken() ~ "'");

View File

@ -0,0 +1,35 @@
module tlang.compiler.symbols.aliases;
import tlang.compiler.symbols.data : Statement;
import tlang.compiler.symbols.expressions : Expression;
import std.string : format;
/**
* A declaration of an alias expression
*/
public final class AliasDeclaration : Statement
{
private string aliasName;
private Expression aliasExpr;
this(string aliasName, Expression aliasExpr)
{
this.aliasName = aliasName;
this.aliasExpr = aliasExpr;
}
public string getName()
{
return this.aliasName;
}
public Expression getExpr()
{
return this.aliasExpr;
}
public override string toString()
{
return format("Alias [name: %s]", this.aliasName);
}
}

View File

@ -305,6 +305,11 @@ public enum SymbolType
*/
SINGLE_LINE_COMMENT,
/**
* `alias` keyword
*/
ALIAS,
/**
* Unknown symbol
*/
@ -715,6 +720,11 @@ public SymbolType getSymbolType(Token tokenIn)
{
return SymbolType.IMPORT;
}
/* `alias` keyword */
else if(cmp("alias", token) == 0)
{
return SymbolType.ALIAS;
}
/* An identifier/type (of some sorts) - further inspection in parser is needed */
else if(isPathIdentifier(token) || isIdentifier(token))
{
@ -841,6 +851,7 @@ public SymbolType getSymbolType(Token tokenIn)
return SymbolType.SMALLER_THAN_OR_EQUALS;
}
return SymbolType.UNKNOWN;
}

View File

@ -390,6 +390,42 @@ public final class Resolver
return resolveWithin(currentContainer, derive_nameMatch(name));
}
public void collectWithin(Container currentContainer, Predicate!(Statement) pred, ref Statement[] collection)
{
Statement[] statements = currentContainer.getStatements();
foreach(Statement statement; statements)
{
if(pred(statement))
{
collection ~= statement;
}
}
}
public void collectUpwards(Container currentContainer, Predicate!(Statement) pred, ref Statement[] collected)
{
// Collect everything at the current level
collectWithin(currentContainer, pred, collected);
// If the current container a Statement
if(cast(Statement)currentContainer)
{
Statement cntnrStmt = cast(Statement)currentContainer;
if(pred(cntnrStmt))
{
collected ~= cntnrStmt;
}
// Does it have a parent?
Container parent = cntnrStmt.parentOf();
if(parent)
{
collectUpwards(parent, pred, collected);
}
}
}
/**
* Performs a horizontal-based search of the given
* `Container`, returning the first `Entity` found

View File

@ -0,0 +1,17 @@
module simple_aliases;
int c = 0;
int cnt()
{
c=c+1;
return c;
}
alias expr = cnt();
int main()
{
int i = cnt();
int p = cnt();
return i+p;
}