* Convert a template AST expression into an output AST expression.
( ast: e.AST, job: CompilationJob, baseSourceSpan: ParseSourceSpan | null, )
| 1061 | * Convert a template AST expression into an output AST expression. |
| 1062 | */ |
| 1063 | function convertAst( |
| 1064 | ast: e.AST, |
| 1065 | job: CompilationJob, |
| 1066 | baseSourceSpan: ParseSourceSpan | null, |
| 1067 | ): o.Expression { |
| 1068 | if (ast instanceof e.ASTWithSource) { |
| 1069 | return convertAst(ast.ast, job, baseSourceSpan); |
| 1070 | } else if (ast instanceof e.PropertyRead) { |
| 1071 | if (ast.receiver instanceof e.ImplicitReceiver) { |
| 1072 | return new ir.LexicalReadExpr(ast.name); |
| 1073 | } else { |
| 1074 | return new o.ReadPropExpr( |
| 1075 | convertAst(ast.receiver, job, baseSourceSpan), |
| 1076 | ast.name, |
| 1077 | null, |
| 1078 | convertSourceSpan(ast.span, baseSourceSpan), |
| 1079 | ); |
| 1080 | } |
| 1081 | } else if (ast instanceof e.Call) { |
| 1082 | if (ast.receiver instanceof e.ImplicitReceiver) { |
| 1083 | throw new Error(`Unexpected ImplicitReceiver`); |
| 1084 | } else { |
| 1085 | return new o.InvokeFunctionExpr( |
| 1086 | convertAst(ast.receiver, job, baseSourceSpan), |
| 1087 | ast.args.map((arg) => convertAst(arg, job, baseSourceSpan)), |
| 1088 | undefined, |
| 1089 | convertSourceSpan(ast.span, baseSourceSpan), |
| 1090 | ); |
| 1091 | } |
| 1092 | } else if (ast instanceof e.LiteralPrimitive) { |
| 1093 | return o.literal(ast.value, undefined, convertSourceSpan(ast.span, baseSourceSpan)); |
| 1094 | } else if (ast instanceof e.Unary) { |
| 1095 | switch (ast.operator) { |
| 1096 | case '+': |
| 1097 | return new o.UnaryOperatorExpr( |
| 1098 | o.UnaryOperator.Plus, |
| 1099 | convertAst(ast.expr, job, baseSourceSpan), |
| 1100 | undefined, |
| 1101 | convertSourceSpan(ast.span, baseSourceSpan), |
| 1102 | ); |
| 1103 | case '-': |
| 1104 | return new o.UnaryOperatorExpr( |
| 1105 | o.UnaryOperator.Minus, |
| 1106 | convertAst(ast.expr, job, baseSourceSpan), |
| 1107 | undefined, |
| 1108 | convertSourceSpan(ast.span, baseSourceSpan), |
| 1109 | ); |
| 1110 | default: |
| 1111 | throw new Error(`AssertionError: unknown unary operator ${ast.operator}`); |
| 1112 | } |
| 1113 | } else if (ast instanceof e.Binary) { |
| 1114 | const operator = BINARY_OPERATORS.get(ast.operation); |
| 1115 | if (operator === undefined) { |
| 1116 | throw new Error(`AssertionError: unknown binary operator ${ast.operation}`); |
| 1117 | } |
| 1118 | return new o.BinaryOperatorExpr( |
| 1119 | operator, |
| 1120 | convertAst(ast.left, job, baseSourceSpan), |
no test coverage detected