Skip to content

Commit 35db74d

Browse files
committed
global 语句的支持
1 parent 9645651 commit 35db74d

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

SimpleShellScript/dotnet.proj/ss/core/Parser.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,13 @@ void ParseStatements(List<SyntaxTree> list)
551551
case (int)TokenType.FUNCTION:
552552
statement = ParseFunctionStatement(); break;
553553
case (int)TokenType.LOCAL:
554-
statement = ParseLocalStatement(); break;
554+
case (int)TokenType.GLOBAL:
555+
{
556+
var state = ParseScopeStatement();
557+
state.is_global = token_ahead.EqualTo(TokenType.GLOBAL);
558+
statement = state;
559+
break;
560+
}
555561
case (int)TokenType.RETURN:
556562
statement = ParseReturnStatement(); break;
557563
case (int)TokenType.BREAK:
@@ -781,21 +787,21 @@ ForInStatement ParseForInStatement()
781787
return statement;
782788
}
783789

784-
SyntaxTree ParseLocalStatement()
790+
ScopeStatement ParseScopeStatement()
785791
{
786792
NextToken();// skip 'local'
787793

788794
if (LookAhead().m_type == (int)TokenType.FUNCTION)
789-
return ParseLocalFunction();
795+
return ParseScopeFunction();
790796
else if (LookAhead().m_type == (int)TokenType.NAME)
791-
return ParseLocalNameList();
797+
return ParseScopeNameList();
792798
else
793-
throw NewParserException("unexpect token after 'local'", _look_ahead);
799+
throw NewParserException("unexpect token after 'local' or 'global'", _look_ahead);
794800
}
795-
LocalFunctionStatement ParseLocalFunction()
801+
ScopeFunctionStatement ParseScopeFunction()
796802
{
797803
NextToken();// skip 'function'
798-
var statement = new LocalFunctionStatement(_current.m_line);
804+
var statement = new ScopeFunctionStatement(_current.m_line);
799805

800806
if (NextToken().m_type != (int)TokenType.NAME)
801807
throw NewParserException("expect 'id' after 'local function'", _current);
@@ -804,9 +810,9 @@ LocalFunctionStatement ParseLocalFunction()
804810
statement.func_body = ParseFunctionBody();
805811
return statement;
806812
}
807-
LocalNameListStatement ParseLocalNameList()
813+
ScopeNameListStatement ParseScopeNameList()
808814
{
809-
var statement = new LocalNameListStatement(_current.m_line);
815+
var statement = new ScopeNameListStatement(_current.m_line);
810816
statement.name_list = ParseNameList();
811817
if (LookAhead().m_type == '=')
812818
{

SimpleShellScript/dotnet.proj/ss/core/SyntaxTree.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,25 @@ public FunctionName(int line_)
141141
public List<Token> names = new List<Token>();
142142
}
143143

144-
public class LocalFunctionStatement : SyntaxTree
144+
public abstract class ScopeStatement : SyntaxTree
145145
{
146-
public LocalFunctionStatement(int line_)
146+
public bool is_global = false;
147+
}
148+
149+
public class ScopeFunctionStatement : ScopeStatement
150+
{
151+
public ScopeFunctionStatement(int line_)
147152
{
148153
_line = line_;
149154
}
155+
150156
public Token name;
151157
public FunctionBody func_body;
152158
}
153159

154-
public class LocalNameListStatement : SyntaxTree
160+
public class ScopeNameListStatement : ScopeStatement
155161
{
156-
public LocalNameListStatement(int line_)
162+
public ScopeNameListStatement(int line_)
157163
{
158164
_line = line_;
159165
}

0 commit comments

Comments
 (0)