22 BinOpNode , ConstNode , AstBlock , Token , ParserOptions , AstNode , Operators , AssignNode , TokenTypes ,
33 GetSingleVarNode , FunctionCallNode , getTokenType , getTokenValue , isTokenTypeLiteral , getStartLine ,
44 getStartColumn , getEndColumn , getEndLine , findOperators , splitTokens , DotObjectAccessNode , BracketObjectAccessNode ,
5- findTokenValueIndex , FunctionDefNode , CreateObjectNode , ObjectPropertyInfo , CreateArrayNode , ArrowFuncDefNode , ExpressionOperators , IfNode , ForNode , WhileNode
5+ findTokenValueIndex , FunctionDefNode , CreateObjectNode , ObjectPropertyInfo , CreateArrayNode , ArrowFuncDefNode , ExpressionOperators , IfNode , ForNode , WhileNode , ImportNode , NameAlias
66} from '../common' ;
77
88export class InstructionLine {
@@ -60,14 +60,15 @@ export class Parser {
6060 }
6161
6262 for ( let i = 0 ; i < instructions . length ; i ++ ) {
63- const instruction = instructions [ i ]
63+ const instruction = instructions [ i ] ;
64+ const firstToken = instruction . tokens [ 0 ] ;
6465
6566 if ( ! instruction . tokens . length ) {
6667 continue ;
6768 }
6869 const assignTokens = splitTokens ( instruction . tokens , '=' ) ;
6970
70- if ( getTokenValue ( instruction . tokens [ 0 ] ) === 'def' ) {
71+ if ( getTokenValue ( firstToken ) === 'def' ) {
7172 const funcName = getTokenValue ( instruction . tokens [ 1 ] ) as string ;
7273 const paramsTokens = instruction . tokens . slice (
7374 instruction . tokens . findIndex ( tkns => getTokenValue ( tkns ) === '(' ) + 1 ,
@@ -91,7 +92,7 @@ export class Parser {
9192
9293 ast . funcs . push ( new FunctionDefNode ( funcName , params , funcAst . body ) )
9394
94- } else if ( getTokenValue ( instruction . tokens [ 0 ] ) === 'if' ) {
95+ } else if ( getTokenValue ( firstToken ) === 'if' ) {
9596
9697 const endDefOfDef = findTokenValueIndex ( instruction . tokens , v => v === ':' ) ;
9798
@@ -111,7 +112,7 @@ export class Parser {
111112
112113 ast . body . push ( new IfNode ( conditionNode , ifBody , elseBody ) )
113114
114- } else if ( getTokenValue ( instruction . tokens [ 0 ] ) === 'for' ) {
115+ } else if ( getTokenValue ( firstToken ) === 'for' ) {
115116
116117 const endDefOfDef = findTokenValueIndex ( instruction . tokens , v => v === ':' ) ;
117118
@@ -125,7 +126,7 @@ export class Parser {
125126
126127 ast . body . push ( new ForNode ( sourceArray , itemVarName , forBody ) )
127128
128- } else if ( getTokenValue ( instruction . tokens [ 0 ] ) === 'while' ) {
129+ } else if ( getTokenValue ( firstToken ) === 'while' ) {
129130
130131 const endDefOfDef = findTokenValueIndex ( instruction . tokens , v => v === ':' ) ;
131132
@@ -138,6 +139,40 @@ export class Parser {
138139
139140 ast . body . push ( new WhileNode ( condition , body ) )
140141
142+ } else if ( getTokenValue ( firstToken ) === 'import' ) {
143+ let asIndex = findTokenValueIndex ( instruction . tokens , v => v === 'as' ) ;
144+ if ( asIndex < 0 ) {
145+ asIndex = instruction . tokens . length ;
146+ }
147+
148+ const module = {
149+ name : instruction . tokens . slice ( 1 , asIndex ) . map ( t => getTokenValue ( t ) ) . join ( '' ) ,
150+ alias : instruction . tokens . slice ( asIndex + 1 ) . map ( t => getTokenValue ( t ) ) . join ( '' ) || undefined
151+ } as NameAlias ;
152+
153+ const body = { } as AstBlock ; // empty for now
154+ ast . body . push ( new ImportNode ( module , body ) )
155+ } else if ( getTokenValue ( firstToken ) === 'from' ) {
156+ let importIndex = findTokenValueIndex ( instruction . tokens , v => v === 'import' ) ;
157+ if ( importIndex < 0 ) {
158+ throw Error ( `'import' must follow 'from'` ) ;
159+ }
160+
161+ const module = {
162+ name : instruction . tokens . slice ( 1 , importIndex ) . map ( t => getTokenValue ( t ) ) . join ( '' )
163+ } as NameAlias ;
164+
165+ const parts = splitTokens ( instruction . tokens . slice ( importIndex + 1 ) , ',' )
166+ . map ( t => {
167+ return {
168+ name : getTokenValue ( t [ 0 ] ) ,
169+ alias : ( t . length === 3 ) ? getTokenValue ( t [ 2 ] ) : undefined
170+ } as NameAlias
171+ } ) ;
172+
173+ const body = { } as AstBlock ; // empty for now
174+
175+ ast . body . push ( new ImportNode ( module , body , parts ) )
141176 } else if ( assignTokens . length > 1 ) {
142177 const target = this . createExpressionNode ( assignTokens [ 0 ] ) ;
143178 const source = this . createExpressionNode ( assignTokens [ 1 ] ) ;
0 commit comments