import * as AST from "../../ast/composite" ; import { nodeFromExpression } from "../create" ; import { Port } from "../ports/port" ; import { SingleValuePort } from "../ports/single" ; import { FlatScope } from "../scopes/flat" ; import { Scope } from "../scopes/scope" ; import * as Types from "../types" ; import { ReadyType } from "../types/ready" ; import * as Values from "../values" ; import { ErrorValue } from "../values/error" ; import { ReadyValue } from "../values/ready" ; import { Node } from "./node" ; export class RecordValue extends Node { private scope : FlatScope ; constructor ( record , scope ) { super ( "Record" ) ; this . scope = new FlatScope ( scope ) ; this . scope . addFromAST ( record . contents ) ; this . scope . definitions . forEach ( ( ports , key ) => { if ( ports . length !== 1 ) { throw new Error ( "Polymorphism among record fields is not allowed" ) ; } } ) ; this . outPorts = [ new SingleValuePort ( ( dataType ) => this . fetchData ( dataType ) ) ] ; } public fetchData ( type ) { if ( ! Types . checkType ( new Types . Record ( new Map ( ) ) , type ) ) { return new Values . ErrorValue ( "Incompartible type, not record" ) ; } const subTypes = new Map ( ) ; this . scope . definitions . forEach ( ( ports , key ) => { subTypes . set ( key , new Types . Some ( ) ) ; } ) ; if ( type instanceof Types . Record ) { type . elements . forEach ( ( value , key ) => { subTypes . set ( key , value ) ; } ) ; } const values = new Map ( ) ; this . scope . definitions . forEach ( ( ports , key ) => { values . set ( key , Values . fetchComplete ( ports [ 0 ] , subTypes . get ( key ) ! ) ) ; subTypes . set ( key , values . get ( key ) ! . type ) ; } ) ; return new Values . Record ( new Types . Record ( subTypes ) , values ) ; } }