mirror of https://github.com/tbklang/tlang.git
Compare commits
5 Commits
5f81ac41db
...
07122472b7
Author | SHA1 | Date |
---|---|---|
Tristan B. Velloza Kildaire | 07122472b7 | |
Tristan B. Velloza Kildaire | b326eeba59 | |
Tristan B. Velloza Kildaire | 63ea709362 | |
Tristan B. Velloza Kildaire | a292809143 | |
Tristan B. Velloza Kildaire | 4f9ac9eb56 |
|
@ -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() ~ "'");
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue