Skip to content

Commit

Permalink
Codegen: string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Mar 22, 2024
1 parent 9dcdb74 commit 048d232
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/codegen/js/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Context } from '../../scope'
import { relTypeName } from '../../scope/trait'
import { operatorImplMap } from '../../semantic/op'
import { ConcreteGeneric } from '../../typecheck'
import { todo, unreachable } from '../../util/todo'
import { unreachable } from '../../util/todo'
import { EmitNode, emitIntersperse, emitToken, emitTree, jsError, jsVariable } from './node'
import { emitBlock, emitBlockStatements } from './statement'

Expand Down Expand Up @@ -270,7 +270,22 @@ export const emitOperand = (operand: Operand, module: Module, ctx: Context): Emi
case 'bool-literal':
return emitLiteral(operand, module, ctx, resultVar)
case 'string-interpolated':
return todo('string interpolation')
const ts: EmitExpr[] = operand.tokens.map(t => {
if (typeof t === 'string') {
return { emit: emitToken(''), resultVar: `"${t}"` }
} else {
const exprEmit = emitExpr(t, module, ctx)
return {
emit: exprEmit.emit,
resultVar: extractValue(`${exprEmit.resultVar}.Show().show(${exprEmit.resultVar})`)
}
}
})
const concatEmit = jsVariable(
resultVar,
emitTree([emitToken('String.String('), emitToken(ts.map(t => t.resultVar).join('+')), emitToken(')')])
)
return { emit: emitTree([...ts.map(t => t.emit), concatEmit]), resultVar }
case 'identifier': {
if (operand.ref?.def.kind === 'method-def') {
if (operand.ref.def.fn.static === true) {
Expand Down
1 change: 1 addition & 0 deletions src/semantic/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const checkOperand = (operand: Operand, ctx: Context): void => {
for (const t of operand.tokens) {
if (typeof t !== 'string') {
checkExpr(t, ctx)
upcast(t, t.type!, show, ctx)
const type = extractConcreteSupertype(t.type!, show.identifier, ctx)
if (!type) {
addError(ctx, typeError(ctx, t, t.type!, show))
Expand Down
2 changes: 1 addition & 1 deletion src/std/list.no
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl <T> Collector<T> for List<T> {

impl Show for List<String> {
fn show(self): String {
"[".concat(self.iter().intersperse(", ").collect<String>()).concat("]")
"[{self.iter().intersperse(", ").collect<String>()}]"
}
}

Expand Down

0 comments on commit 048d232

Please sign in to comment.