Skip to content
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

Make scripts be root chunks that can have local statements #3

Merged
merged 4 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Core/EVIL.Grammar/AST/Base/TopLevelStatement.cs

This file was deleted.

9 changes: 7 additions & 2 deletions Core/EVIL.Grammar/AST/Miscellaneous/ProgramNode.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using EVIL.Grammar.AST.Base;
using EVIL.Grammar.AST.Statements.TopLevel;

namespace EVIL.Grammar.AST.Miscellaneous
{
public sealed class ProgramNode : AstNode
{
public List<TopLevelStatement> Statements { get; }
public IEnumerable<Statement> FnStatements => Statements.Where(x => x is FnStatement);
public IEnumerable<Statement> AnythingButFnStatements => Statements.Where(x => x is not FnStatement);

public List<Statement> Statements { get; }

public ProgramNode(List<TopLevelStatement> statements)
public ProgramNode(List<Statement> statements)
{
Statements = statements;
Reparent(Statements);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace EVIL.Grammar.AST.Statements.TopLevel
{
public class AttributeListNode : TopLevelStatement
public class AttributeListNode : Statement
{
public List<AttributeNode> Attributes { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace EVIL.Grammar.AST.Statements.TopLevel
{
public sealed class FnStatement : TopLevelStatement
public sealed class FnStatement : Statement
{
public IdentifierNode Identifier { get; }
public ParameterList ParameterList { get; }
Expand Down
14 changes: 0 additions & 14 deletions Core/EVIL.Grammar/AST/Statements/TopLevel/IncludeStatement.cs

This file was deleted.

4 changes: 4 additions & 0 deletions Core/EVIL.Grammar/Parsing/Base/Parser.Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ private Statement Statement()

switch (token.Type)
{
case TokenType.AttributeList:
case TokenType.Fn:
return FnStatement();

case TokenType.If:
return IfCondition();

Expand Down
41 changes: 0 additions & 41 deletions Core/EVIL.Grammar/Parsing/Base/Parser.TopLevelStatement.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace EVIL.Grammar.Parsing
{
public partial class Parser
{
private IndexerExpression Indexing(Expression indexable)
private IndexerExpression IndexerExpression(Expression indexable)
{
int line, col;
Expression indexer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace EVIL.Grammar.Parsing
{
public partial class Parser
{
private InvocationExpression FunctionCall(Expression callee)
private InvocationExpression InvocationExpression(Expression callee)
{
var (line, col) = (_lexer.State.Line, _lexer.State.Column);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ private Expression PostfixExpression()

if (token.Type == TokenType.LParenthesis)
{
node = FunctionCall(node);
node = InvocationExpression(node);
}
else if (token.Type == TokenType.LBracket || token.Type == TokenType.Dot)
{
node = Indexing(node);
node = IndexerExpression(node);
}
else if (token.Type == TokenType.DoubleColon)
{
Expand Down
4 changes: 2 additions & 2 deletions Core/EVIL.Grammar/Parsing/Miscellaneous/Parser.Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class Parser
{
private ProgramNode Program()
{
var statementList = new List<TopLevelStatement>();
var statementList = new List<Statement>();

if (CurrentToken == Token.Empty)
{
Expand All @@ -22,7 +22,7 @@ private ProgramNode Program()
while (CurrentToken.Type != TokenType.EOF)
{
statementList.Add(
TopLevelStatement()
Statement()
);
}

Expand Down
2 changes: 1 addition & 1 deletion Core/EVIL.Grammar/Parsing/Statements/Parser.EachLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private EachStatement EachLoop()
if (CurrentToken == Token.Val)
{
throw new ParserException(
"Each-loop variables must be `rw'.",
"Each-loop variables must be 'rw'.",
(_lexer.State.Line, _lexer.State.Column)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ private FnStatement FnStatement()
}

var (line, col) = Match(Token.Fn);
if (_functionDescent > 0)
{
throw new ParserException(
"Named function definitions may only appear outside of other functions.",
(line, col)
);
}

var functionIdentifier = Identifier();
var parameterList = ParameterList();

Expand Down

This file was deleted.

2 changes: 0 additions & 2 deletions Core/EVIL.Grammar/Traversal/AstVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ protected AstVisitor()
#nullable disable
Handlers = new()
{
{ typeof(IncludeStatement), (n) => Visit((IncludeStatement)n) },
{ typeof(ArgumentList), (n) => Visit((ArgumentList)n) },
{ typeof(ParameterList), (n) => Visit((ParameterList)n) },
{ typeof(BlockStatement), (n) => Visit((BlockStatement)n) },
Expand Down Expand Up @@ -85,7 +84,6 @@ public virtual void Visit(AstNode node)
public abstract void Visit(ProgramNode programNode);
public abstract void Visit(ParameterList parameterList);
public abstract void Visit(ArgumentList argumentList);
public abstract void Visit(IncludeStatement includeStatement);
public abstract void Visit(BlockStatement blockStatement);
public abstract void Visit(ConditionalExpression conditionalExpression);
public abstract void Visit(CoalescingExpression coalescingExpression);
Expand Down
10 changes: 0 additions & 10 deletions Core/EVIL.Lexical/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ public void NextToken()
State.PreviousToken = State.CurrentToken;
var (line, col) = (State.Line, State.Column);

if (col == 1)
{
if (State.Character == '#' && PeekString(7) == "include")
{
Advance(8);
State.CurrentToken = Token.Include with { Line = line, Column = col };
return;
}
}

switch (State.Character)
{
case '.' when Peek() == '.' && Peek(2) == '.':
Expand Down
1 change: 0 additions & 1 deletion Core/EVIL.Lexical/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public override int GetHashCode()
public static readonly Token HexInteger = new(TokenType.HexInteger, TokenClass.Literal, string.Empty);
public static readonly Token String = new(TokenType.String, TokenClass.Literal, string.Empty);

public static readonly Token Include = new(TokenType.Include, TokenClass.Meta, "#include");
public static readonly Token EOF = new(TokenType.EOF, TokenClass.Meta, "<EOF>");
public static readonly Token Empty = new(TokenType.Empty, TokenClass.Meta, string.Empty);
}
Expand Down
3 changes: 1 addition & 2 deletions Core/EVIL.Lexical/TokenType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ public enum TokenType
GetOverride,
SetOverride,
ExistsOverride,

Include,

EOF
}
}
10 changes: 0 additions & 10 deletions Core/Tests/EVIL.CoreTests/LexerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,5 @@ public void TypeCodes()
Expect(NativeFunctionTypeCode, NativeObjectTypeCode);
Expect(EOF);
}

[Test]
public void Meta()
{
_lexer.LoadSource("#include \"test.vil\"");

Expect(Include);
ExpectExact((TokenType.String, "test.vil"));
Expect(EOF);
}
}
}
8 changes: 0 additions & 8 deletions FrontEnd/EVIL.evil/AttributeNames.cs

This file was deleted.

Loading