-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cae56d4
commit 50b3d7f
Showing
13 changed files
with
92 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { extractValue, jsRelName, nextVariable } from '.' | ||
import { Module } from '../../ast' | ||
import { Context } from '../../scope' | ||
import { trace } from '../../scope/std' | ||
import { InstanceRelation, resolveTypeImpl } from '../../scope/trait' | ||
import { todo } from '../../util/todo' | ||
import { EmitNode, emitToken, emitTree, jsVariable } from './node' | ||
|
||
export const emitTraceImpl = (rel: InstanceRelation, module: Module, ctx: Context): EmitNode => { | ||
const typeDef = rel.forDef.def | ||
if (typeDef.kind === 'trait-def') { | ||
return todo('default Trace for trait type') | ||
} | ||
const variants = typeDef.variants.map(v => { | ||
const fields = v.fieldDefs.map(f => { | ||
const impl = resolveTypeImpl(f.type!, trace, ctx) | ||
if (impl) { | ||
ctx.moduleStack.at(-1)!.relImports.push(impl.impl) | ||
} | ||
const fAccess = `${extractValue('self')}.${f.name.value}` | ||
const fieldTraceCall = impl | ||
? `${jsRelName(impl.impl)}().trace(${fAccess})` | ||
: `${fAccess}.Trace().trace(${fAccess})` | ||
return `"${f.name.value}: "+${extractValue(fieldTraceCall)}` | ||
}) | ||
const variantStr = | ||
fields.length > 0 ? [`"${v.name.value}("`, fields.join('+", "+'), '")"'].join('+') : `"${v.name.value}()"` | ||
return `if(self.$noisVariant==="${v.name.value}"){return String.String(${variantStr});}` | ||
}) | ||
const fnStr = `function(self){${variants.join('')}}` | ||
const fnBodyEmit = emitToken(`trace: ${fnStr}`) | ||
const fnEmit = emitTree([emitToken('{'), fnBodyEmit, emitToken('};')]) | ||
|
||
const cached = nextVariable(ctx) | ||
const instanceEmit = emitTree([ | ||
emitToken(`function(){if(${cached}){return ${cached};}${cached}=`), | ||
fnEmit, | ||
emitToken(`return ${cached};}`) | ||
]) | ||
return emitTree([jsVariable(cached), jsVariable(jsRelName(rel), instanceEmit, true)]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
pub trait Trace { | ||
fn trace(self): String { | ||
trace(self) | ||
panic("default impl of `Trace::trace` is created for each implementing type by the compiler") | ||
} | ||
} | ||
|
||
fn trace<T>(value: T): String |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters