11import {
2- BinOpNode , ConstNode , AstBlock , Token , ParserOptions , AstNode , Operators , AssignNode , TokenTypes ,
3- GetSingleVarNode , FunctionCallNode , getTokenType , getTokenValue , isTokenTypeLiteral , getStartLine ,
4- getStartColumn , getEndColumn , getEndLine , findOperators , splitTokens , DotObjectAccessNode , BracketObjectAccessNode ,
5- findTokenValueIndex , FunctionDefNode , CreateObjectNode , ObjectPropertyInfo , CreateArrayNode , ArrowFuncDefNode , ExpressionOperators
2+ BinOpNode , ConstNode , AstBlock , Token , ParserOptions , AstNode , Operators , AssignNode , TokenTypes ,
3+ GetSingleVarNode , FunctionCallNode , getTokenType , getTokenValue , isTokenTypeLiteral , getStartLine ,
4+ getStartColumn , getEndColumn , getEndLine , findOperators , splitTokens , DotObjectAccessNode , BracketObjectAccessNode ,
5+ findTokenValueIndex , FunctionDefNode , CreateObjectNode , ObjectPropertyInfo , CreateArrayNode , ArrowFuncDefNode , ExpressionOperators , IfNode
66} from '../common' ;
77
88export class InstructionLine {
@@ -52,12 +52,19 @@ export class Parser {
5252
5353 private instructionsToNodes ( instructions : InstructionLine [ ] , ast : AstBlock ) : void {
5454
55- for ( let instruction of instructions ) {
55+ const getBody = ( tokens : Token [ ] , startTokenIndex : number ) : AstNode [ ] => {
56+ const instructionLines = this . getBlock ( tokens , getStartLine ( tokens [ startTokenIndex ] ) ) ;
57+ const bodyAst = { body : [ ] as AstNode [ ] , funcs : [ ] as AstNode [ ] } as AstBlock ;
58+ this . instructionsToNodes ( instructionLines , bodyAst ) ;
59+ return bodyAst . body ;
60+ }
61+
62+ for ( let i = 0 ; i < instructions . length ; i ++ ) {
63+ const instruction = instructions [ i ]
5664
5765 if ( ! instruction . tokens . length ) {
5866 continue ;
5967 }
60-
6168 const assignTokens = splitTokens ( instruction . tokens , '=' ) ;
6269
6370 if ( getTokenValue ( instruction . tokens [ 0 ] ) === 'def' ) {
@@ -84,6 +91,26 @@ export class Parser {
8491
8592 ast . funcs . push ( new FunctionDefNode ( funcName , params , funcAst . body ) )
8693
94+ } else if ( getTokenValue ( instruction . tokens [ 0 ] ) === 'if' ) {
95+
96+ const endDefOfDef = findTokenValueIndex ( instruction . tokens , v => v === ':' ) ;
97+
98+ if ( endDefOfDef === - 1 ) {
99+ throw ( `Can't find : for if` )
100+ }
101+
102+ const ifBody = getBody ( instruction . tokens , endDefOfDef + 1 ) ;
103+ const conditionNode = this . createExpressionNode ( instruction . tokens . slice ( 1 , endDefOfDef ) )
104+
105+ let elseBody : AstNode [ ] | undefined = undefined ;
106+ if ( getTokenValue ( instructions [ i + 1 ] . tokens [ 0 ] ) === 'else'
107+ && getTokenValue ( instructions [ i + 1 ] . tokens [ 1 ] ) === ':' ) {
108+ elseBody = getBody ( instructions [ i + 1 ] . tokens , 2 ) ;
109+ i ++ ;
110+ }
111+
112+ ast . body . push ( new IfNode ( conditionNode , ifBody , elseBody ) )
113+
87114 } else if ( assignTokens . length > 1 ) {
88115 const target = this . createExpressionNode ( assignTokens [ 0 ] ) ;
89116 const source = this . createExpressionNode ( assignTokens [ 1 ] ) ;
@@ -112,7 +139,7 @@ export class Parser {
112139 currentLine = sLine ;
113140 }
114141
115- if ( column === sColumn && ! ")}]" . includes ( value as string ) ) {
142+ if ( column === sColumn && ! ")}]" . includes ( value as string ) ) {
116143 currentLine = sLine ;
117144 lines . push ( line ) ;
118145 line = new InstructionLine ( ) ;
@@ -164,7 +191,7 @@ export class Parser {
164191
165192 // arrow function
166193 const arrowFuncParts = splitTokens ( tokens , '=>' ) ;
167- if ( arrowFuncParts . length > 1 ) {
194+ if ( arrowFuncParts . length > 1 ) {
168195 const params = splitTokens ( arrowFuncParts [ 0 ] , ',' ) . map ( t => getTokenValue ( t [ 0 ] ) as string ) ;
169196
170197 const instructionLines = this . getBlock ( arrowFuncParts [ 1 ] , 0 ) ;
@@ -275,7 +302,7 @@ export class Parser {
275302 }
276303
277304 // unquoted string becomes a variable, so, we don't need it, that is why we are creating const node explicitlely
278- const name = ( keyValue [ 0 ] . length === 1 && ! `'"` . includes ( ( getTokenValue ( keyValue [ 0 ] [ 0 ] ) as string ) [ 0 ] ) )
305+ const name = ( keyValue [ 0 ] . length === 1 && ! `'"` . includes ( ( getTokenValue ( keyValue [ 0 ] [ 0 ] ) as string ) [ 0 ] ) )
279306 ? new ConstNode ( keyValue [ 0 ] [ 0 ] )
280307 : this . createExpressionNode ( keyValue [ 0 ] ) ;
281308 const pInfo = {
0 commit comments