# HG changeset patch # User Mike Pavone # Date 1311826463 25200 # Node ID 24c6f876719070afcd6948d0fd079955f598f787 # Parent db30bda91aad342c0cc5e9f0292ceb73652e94ab Remove obsolete version of compiler that ran under the interpreter diff -r db30bda91aad -r 24c6f8767190 backendutils.rhope --- a/backendutils.rhope Wed Jul 27 21:06:57 2011 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,353 +0,0 @@ - -Escape Rhope Name NU[name:escaped] -{ - escaped <- [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[name]Replace["_","UN_"] - ]Replace["@","AT_"] - ]Replace[" ","SP_"] - ]Replace[":","CN_"] - ]Replace["?","QN_"] - ]Replace["+","PL_"] - ]Replace["-","MN_"] - ]Replace["*","TM_"] - ]Replace["/","DV_"] - ]Replace["<","LT_"] - ]Replace[">","GT_"] - ]Replace["(","LP_"] - ]Replace[")","RP_"] - ]Replace["!","NT_"] - ]Replace["=","EQ_"] - ]Replace["'","PR_"] - ]Replace["\"","DP_"] - ]Replace["\t","TB_"] - ]Replace[",", "CM_"] - ]Replace[".", "PD_"] - ]Replace["\n", "NL_"] - ]Replace["{", "LC_"] - ]Replace["}", "RC_"] - ]Replace["[", "LS_"] - ]Replace["]", "RS_"] - ]Replace["#", "HS_"] - ]Replace["\\", "BS_"] - ]Replace["\r", "CR_"] - ]Replace[";", "SC_"] - ]Replace["&", "AM_"] - ]Replace["|", "PI_"] - ]Replace["%", "PC_"] - ]Replace["`", "BT_"] - ]Replace["~", "TD_"] -} - -Escape Rhope Name[name:out] -{ - escaped <- Escape Rhope Name NU[[name]Replace["_","__"]] - [("char","int","short","long","float","double","void","struct")]Find[escaped] - { - out <- ["ID_"]Append[escaped] - }{ - out <- Val[escaped] - } -} - -Blueprint AddRef -{ - Value -} - -AddRef[value:out] -{ - out <- [Build["AddRef"]]Value <<[value] -} - -Make Op@AddRef[addref,func:out] -{ - //TODO: Make me work with other backends - out <- [["add_ref((object *)"]Append[ [[addref]Value >>]Make Op[func] ]]Append[")"] -} - -Strip Addref@AddRef[op:out] -{ - out <- [[op]Value >>]Strip Addref -} - -Make Op@String[string,func:out] -{ - out <- [func]Resolve[string] -} - -Strip Addref@String[op:out] -{ - out <- op -} - -Make Op@Whole Number[num,func:out] -{ - out <- num -} - -Strip Addref@Whole Number[op:out] -{ - out <- op -} - -Make Op@Real Number[num,func:out] -{ - out <- num -} - -Strip Addref@Real Number[op:out] -{ - out <- op -} - -Blueprint Output -{ - Name -} - -Output[name:out] -{ - out <- [Build["Output"]]Name <<[name] -} - -Make Op@Output[op,func:out] -{ - out <- [func]Resolve Output[[op]Name >>] -} - -Strip Addref@Output[op:out] -{ - out <- op -} - -Blueprint Constant -{ - Value - Need Addref -} - -Constant[var:out] -{ - out <- [[Build["Constant"]]Value <<[var]]Need Addref <<[Yes] -} - -Make Op@Constant[const,func:out] -{ - out <- [func]Lookup Constant[[const]Value >>, [const]Need Addref >>] -} - -Strip Addref@Constant[op:out] -{ - out <- [op]Need Addref <<[No] -} - -Blueprint Result -{ - Output Num -} - -Result[num:out] -{ - out <- [Build["Result"]]Output Num <<[num] -} - -Make Op@Result[result,func:out] -{ - out <- [func]Result Reference[[result]Output Num>>] -} - -Strip Addref@Result[op:out] -{ - out <- op -} - -Blueprint Check Result -{ - Output Num -} - -Check Result[num:out] -{ - out <- [Build["Check Result"]]Output Num <<[num] -} - -Make Op@Check Result[result,func:out] -{ - out <- [func]Checked Result Reference[[result]Output Num>>] -} - -Strip Addref@Check Result[op:out] -{ - out <- op -} - -Make Condition[op:out] -{ - If[[Type Of[op]]=["OrValue"]] - { - out <- OrCond[Make Condition[[op]Left >>], Make Condition[[op]Right >>]] - }{ - out <- op - } -} - -Blueprint OrValue -{ - Left - Right -} - -OrValue[left,right:out] -{ - out <- [[Build["OrValue"]]Left <<[left]]Right <<[right] -} - -Make Op@OrValue[orval,func:out] -{ - out <- [func]If Null Else[[orval]Left >>, [orval]Right >>] -} - -Strip Addref@OrValue[op:out] -{ - out <- [[op]Left <<[ [[op]Left >>]Strip Addref ]]Right <<[ [[op]Right >>]Strip Addref ] -} - -Blueprint NotCond -{ - Condition -} - -NotCond[cond:out] -{ - out <- [Build["NotCond"]]Condition <<[[cond]Strip Addref] -} - -Make Op@NotCond[cond,func:out] -{ - out <- ["!"]Append[[[cond]Condition >>]Make Op[func]] -} - -Strip Addref@NotCond[op:out] -{ - out <- op -} - -Blueprint OrCond -{ - Condition1 - Condition2 -} - -OrCond[cond1,cond2:out] -{ - out <- [[Build["OrCond"]]Condition1 <<[[cond1]Strip Addref]]Condition2 <<[[cond2]Strip Addref] -} - -Make Op@OrCond[cond,func:out] -{ - out <- ["("]Append[[[ [[cond]Condition1 >>]Make Op[func] ]Append[" || "]]Append[[ [[cond]Condition2 >>]Make Op[func] ]Append[")"]]] -} - -Strip Addref@OrCond[op:out] -{ - out <- op -} - -Blueprint AndCond -{ - Condition1 - Condition2 -} - -AndCond[cond1,cond2:out] -{ - out <- [[Build["AndCond"]]Condition1 <<[[cond1]Strip Addref]]Condition2 <<[[cond2]Strip Addref] -} - -Make Op@AndCond[cond,func:out] -{ - out <- ["("]Append[[[ [[cond]Condition1 >>]Make Op[func] ]Append[" && "]]Append[[ [[cond]Condition2 >>]Make Op[func] ]Append[")"]]] -} - -Strip Addref@AndCond[op:out] -{ - out <- op -} - -Blueprint Field Ref -{ - Variable - Field -} - -Field Ref[var,field:out] -{ - out <- [[Build["Field Ref"]]Variable <<[var]]Field <<[field] -} - -Make Op@Field Ref[ref,func:out] -{ - out <- [func]Field Result[[ref]Variable >>,[ref]Field >>] -} - -Strip Addref@Field Ref[op:out] -{ - out <- op -} - -Blueprint Type Instance -{ - Name - Params - Variant - Mutable? -} - -Type Instance[raw name:out] -{ - If[[raw name]=[""]] - { - name <- "Any Type" - }{ - name <- raw name - } - out <- [[[[Build["Type Instance"]]Name <<[name]]Params <<[()]]Variant <<["Boxed"]]Mutable? <<[No] -} - -Set Variant@Type Instance[type,variant:out,invalid] -{ - [("Boxed","Naked","Pointer","Raw Pointer")]Find[variant] - { - out <- [type]Variant <<[variant] - }{ - invalid <- type - } -} - -=@Type Instance[type,compare:out] -{ - If[[Type Of[compare]] = ["String"]] - { - out <- [[type]Name >>] = [compare] - }{ - //TODO: Compare parameters - ,out <- If[[[type]Name >>] = [[compare]Name >>]] - { out <- [[type]Variant >>] = [[compare]Variant >>] } - } -} - -Blueprint Worker Literal -{ - Name - Args -} - -Worker Literal[name:out] -{ - out <- [[Build["Worker Literal"]]Name <<[name]]Args <<[()] -} - -Set Input@Worker Literal[worker,argnum,val:out] -{ - out <- [worker]Args <<[ [[worker]Args >>]Set[argnum, val] ] -} - diff -r db30bda91aad -r 24c6f8767190 cbackend.rhope --- a/cbackend.rhope Wed Jul 27 21:06:57 2011 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1699 +0,0 @@ -Import extendlib.rhope -Import backendutils.rhope - -Blueprint Blueprint Def -{ - Name - Fixed Size - Fields - Methods -} - -Blueprint Def[name] -{ - out <- [[[[Build["Blueprint Def"]]Name <<[name]]Fixed Size <<[0]]Fields <<[()]]Methods <<[Dictionary[]] -} - -Blueprint C Method Registry -{ - Lookup - Next ID -} - -C Method Registry[:out] -{ - builtins <- [[[[[[[[[[[[[Dictionary[] - ]Set["+", "METHOD_ADD"] - ]Set["-", "METHOD_SUB"] - ]Set["/", "METHOD_DIV"] - ]Set["*", "METHOD_MUL"] - ]Set["LShift", "METHOD_LSHIFT"] - ]Set["RShift", "METHOD_RSHIFT"] - ]Set["=", "METHOD_EQUALS"] - ]Set[">", "METHOD_GREATER"] - ]Set["<", "METHOD_LESS"] - ]Set["If", "METHOD_IF"] - ]Set["Set Missing Field", "METHOD_SETFIELDMISSING"] - ]Set["Get Missing Field", "METHOD_GETFIELDMISSING"] - ]Set["Missing Method", "METHOD_MISSING"] - out <- [[Build["C Method Registry"]]Lookup <<[builtins]]Next ID<<[0] - -} - -Register Method@C Method Registry[reg,method:out] -{ - [[reg]Lookup >>]Index[method] - { - out <- reg - }{ - method ID <- [reg]Next ID>> - new lookup <- [[reg]Lookup >>]Set[method, ["METHOD_FIRST_USER+"]Append[method ID]] - out <- [[reg]Lookup <<[new lookup]]Next ID <<[[method ID]+[1]] - } -} - -Method ID@C Method Registry[reg,method:out,notfound] -{ - out,notfound <- [[reg]Lookup >>]Index[method] -} - -Blueprint C Field Registry -{ - Lookup - Next ID -} - -C Field Registry[:out] -{ - out <- [[Build["C Field Registry"]]Lookup <<[Dictionary[]]]Next ID<<[1] - -} - -Register Field@C Field Registry[reg,field:out] -{ - [[reg]Lookup >>]Index[field] - { - out <- reg - }{ - field ID <- [reg]Next ID>> - new lookup <- [[reg]Lookup >>]Set[field, field ID] - out <- [[reg]Lookup <<[new lookup]]Next ID <<[[field ID]+[1]] - } -} - -Field ID@C Field Registry[reg,field:out,notfound] -{ - out,notfound <- [[reg]Lookup >>]Index[field] -} - -Blueprint C Type -{ - Name - Fields - Methods - Init - Copy - Cleanup - -} - -C Type[name:out] -{ - out <- [[[[[[Build["C Type"]]Name <<[name]]Fields <<[()]]Methods <<[()]]Init <<["NULL"]]Copy <<["NULL"]]Cleanup <<["NULL"] -} - -Add Field@C Type[ctype,name,type:out] -{ - out <- [ctype]Fields <<[ [[ctype]Fields >>]Append[ [[()]Append[name]]Append[type] ] ] -} - -Add Method@C Type[ctype,name:out] -{ - out <- [ctype]Methods <<[ [[ctype]Methods >>]Append[name] ] -} - -Register Methods@C Type[ctype,method reg:out] -{ - out <- Fold["Register Method", method reg, [ctype]Methods >>] -} - -_Register Field C[reg,field:out] -{ - name <- [field]Index[0] - out <- [reg]Register Field[name] -} - -Register Fields@C Type[ctype,field reg:out] -{ - out <- Fold["_Register Field C", field reg, [ctype]Fields >>] -} - -Rhope Type to C[type:out,array] -{ - If[[Type Of[type]]=["Type Instance"]] - { - variant <- [type]Variant >> - If[[[type]Name >>] = ["Array"]] - { - [("Naked","Raw Pointer")]Find[variant] - { - /* - //Below code assumes that paramaterized types are implemented - pre param <- [[type]Params >>]Index[0] {} - { pre param <- Type Instance["Any Type"] } - [[type]Params >>]Index[1] - { param,param <- [pre param]Set Variant[~] } - { param <- Val[pre param] } - child type <- Rhope Type to C[param] - If[[variant] = ["Naked"]] - { - out <- Val[child type] - array <- "[1]" - }{ - out <- [child type]Append[" *"] - array <- "" - } */ - out <- "void *" - array <- "" - }{ - typename <- "Array" - } - primitive <- No - }{ - ,regulartype <- [("Naked","Raw Pointer")]Find[variant] - { - [("Int64","Int32","Int16","Int8")]Find[[type]Name >>] - { - primitive <- Yes - [[type]Name >>]Slice[3] {} - { typename <- [["int"]Append[~]]Append["_t"] } - }{ - ,regulartype <- [("UInt64","UInt32","UInt16","UInt8")]Find[[type]Name >>] - { - primitive <- Yes - [[type]Name >>]Slice[4] {} - { typename <- [["uint"]Append[~]]Append["_t"] } - } - } - } - - Val[regulartype] - { - typename <- [type]Name >> - primitive <- No - } - } - }{ - typename <- type - param <- "Any Type" - variant <- "boxed" - primitive <- No - } - Val[typename] - { array <- "" } - If[[typename] = ["Any Type"]] - { - out <- "struct object *" - }{ - [("Naked","Raw Pointer")]Find[variant] - { - If[primitive] - { - prefix <- "" - }{ - prefix <- "nt_" - } - }{ - prefix <- "t_" - } - - If[[variant]=["Naked"]] - { - postfix <- "" - }{ - postfix <- " *" - } - } - If[primitive] - { - escaped <- Val[typename] - }{ - escaped <- Escape Rhope Name[typename] - } - out <- [[prefix]Append[escaped]]Append[postfix] -} - -_Type Def C Type[text,field:out] -{ - name <- [field]Index[0] - ,postfix <- Rhope Type to C[[field]Index[1]] - { type <- ["\n\t"]Append[~] } - - out <- [[[[text]Append[type]]Append[" "]]Append[[Escape Rhope Name[name]]Append[postfix]]]Append[";"] -} - -Type Def@C Type[ctype:out] -{ - If[[[[ctype]Fields >>]Length] = [1]] - { - out <- [[[[_Type Def C Type["typedef struct {\n\tobject _SP_header;\n\t", [[ctype]Fields >>]Index[0]]]Append["\n} t_"]]Append[Escape Rhope Name[[ctype]Name >>]]]Append[";"] - ]Append[ - [[[["typedef " - ]Append[Rhope Type to C[ [[[ctype]Fields >>]Index[0]]Index[1] ]] - ]Append[" nt_"] - ]Append[Escape Rhope Name[[ctype]Name >>]] - ]Append[";"] ] - }{ - //HACK!!! - If[[[ctype]Name >>]=["Blueprint"]] - { - out <- "" - }{ - [("Array","Boxed Array","Worker")]Find[[ctype]Name >>] - { oend <- "\nMObject(" } - { oend <- "\nObject(" } - out <- [Fold["_Type Def C Type", "OBegin", [ctype]Fields >>]]Append[ [[oend]Append[Escape Rhope Name[[ctype]Name >>]]]Append[")"] ] - } - } -} - -_Type Init C[type name,method reg,text,method:out] -{ - out <- [[text]Append[[["\n\tadd_method(bp, "]Append[ [method reg]Method ID[method] ]]Append[ [[", MethodName("]Append[Escape Rhope Name[method]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"] -} - -_Type Init C Field[type name,field reg,text,field:out] -{ - fname <- [field]Index[0] - out <- [[[[text]Append[[["\n\tadd_getter(bp, "]Append[ [field reg]Field ID[fname] ]]Append[ [[", MethodName("]Append[Escape Rhope Name[[fname]Append[" >>"]]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"] - ]Append[[["\n\tadd_setter(bp, "]Append[ [field reg]Field ID[fname] ]]Append[ [[", MethodName("]Append[Escape Rhope Name[[fname]Append[" <<"]]]]Append[[","]Append[Escape Rhope Name[type name]]]]]]Append["));"] -} - -Type Init@C Type[ctype,id,method reg,field reg:out] -{ - [("Array","Boxed Array", "Worker")]Find[[ctype]Name >>] - { size <- "-1" } - { - [("Int64","Int32","Int16","Int8")]Find[[ctype]Name >>] - { - [[ctype]Name >>]Slice[3] {} - { typename <- [["int"]Append[~]]Append["_t"] } - }{ - [("UInt64","UInt32","UInt16","UInt8")]Find[[ctype]Name >>] - { - [[ctype]Name >>]Slice[4] {} - { typename <- [["uint"]Append[~]]Append["_t"] } - }{ - If[[[ctype]Name >>]=["Blueprint"]] - { typename <- "blueprint *" } - { - If[[[ctype]Name >>]=["Boolean"]] - { typename <- "int32_t" } - { typename <- ["nt_"]Append[Escape Rhope Name[[ctype]Name >>]] } - } - } - } - size <- [["sizeof("]Append[typename]]Append[")"] - } - start <- [["\tbp = register_type_byid(" - ]Append[id] - ]Append[ - [[", "]Append[size] - ]Append[ - [", (special_func)"]Append[ - [ - [[[[Escape Rhope Name NU[[ctype]Init >>] - ]Append[", (special_func)"] - ]Append[Escape Rhope Name NU[[ctype]Copy >> ]] - ]Append[", (special_func)"] - ]Append[Escape Rhope Name NU[[ctype]Cleanup >>]] - ]Append[");"]]] ] - out <- Val[start]//Fold[[["_Type Init C Field"]Set Input[0, [ctype]Name >>]]Set Input[1, field reg], Fold[[["_Type Init C"]Set Input[0, [ctype]Name >>]]Set Input[1, method reg], start, [ctype]Methods >>], [ctype]Fields >>] -} - -Blueprint C Type Registry -{ - Lookup - Definitions - Next ID -} - -C Type Registry[:out] -{ - out <- [[[Build["C Type Registry"]]Lookup << [ - [[[[[[[[[[[[[[[[[[[Dictionary[] - ]Set["UInt8", "TYPE_UINT8"] //1 - ]Set["UInt16", "TYPE_UINT16"] //2 - ]Set["UInt32", "TYPE_UINT32"] //3 - ]Set["UInt64", "TYPE_UINT64"] //4 - ]Set["Int8", "TYPE_INT8"] //5 - ]Set["Int16", "TYPE_INT16"] //6 - ]Set["Int32", "TYPE_INT32"] //7 - ]Set["Int64", "TYPE_INT64"] //8 - ]Set["Boolean", "TYPE_BOOLEAN"] //9 - ]Set["Float32", "TYPE_FLOAT32"] //10 - ]Set["Float64", "TYPE_FLOAT64"] //11 - ]Set["Real Number", "TYPE_FLOAT64"] - ]Set["Blueprint", "TYPE_BLUEPRINT"] //12 - ]Set["Array", "TYPE_ARRAY"] //13 - ]Set["Boxed Array", "TYPE_BOXEDARRAY"]//14 - ]Set["Worker", "TYPE_WORKER"] //15 - ]Set["Method Missing Exception", "TYPE_METHODMISSINGEXCEPTION"] //16 - ]Set["Field Missing Exception", "TYPE_FIELDMISSINGEXCEPTION"] //17 - ]Set["Wrong Type Exception", "TYPE_WRONGTYPEEXCEPTION"]] //18 - ]Definitions << [Dictionary[]] - ]Next ID <<[0] -} - -_Type Defs C[text,def:out] -{ - out <- [[text]Append[[def]Type Def]]Append["\n\n"] -} - -Type Defs@C Type Registry[reg:out] -{ - out <- Fold["_Type Defs C", "", [reg]Definitions >>] -} - -_Type Inits C[reg,method reg,field reg,text,def,name:out] -{ - out <- [[text]Append[ [def]Type Init[[reg]Type ID[name], method reg, field reg] ]]Append["\n\n"] -} - -Type Inits@C Type Registry[reg,method reg,field reg:out] -{ - out <- Fold[[[["_Type Inits C"]Set Input[0, reg]]Set Input[1, method reg]]Set Input[2, field reg], "", [reg]Definitions >>] -} - -Register Type@C Type Registry[reg,def:out] -{ - name <- [def]Name >> - [[reg]Lookup >>]Index[name] - { - [[reg]Definitions >>]Index[name] - { - out <- reg - }{ - out <- [reg]Definitions <<[[[reg]Definitions >>]Set[name, def]] - } - }{ - out <- [[[reg]Lookup <<[ [[reg]Lookup >>]Set[name, ["TYPE_FIRST_USER+"]Append[[reg]Next ID >>]] ] - ]Definitions <<[ [[reg]Definitions >>]Set[name, def] ] - ]Next ID <<[ [[reg]Next ID >>]+[1] ] - } -} - -Type ID@C Type Registry[reg,name:out,notfound] -{ - out <- [[reg]Lookup >>]Index[name] {} - { - ,notfound <- If[[name]=["Any Type"]] - { out <- "0" } - } -} - -Simple Type?@C Type Registry[reg,name:yep,nope,notfound] -{ - ,notfound <- [[reg]Definitions >>]Index[name] - { - yep,nope <- If[[[[~]Fields >>]Length] = [1]] - } -} - -Defined?@C Type Registry[reg,name:yep,nope] -{ - yep,nope <- [[reg]Definitions >>]Index[name] -} - -Blueprint C Function -{ - Name - Inputs - Outputs - Convention - Variables - Statements - Method Registry - Field Registry - Type Registry - Constants - Input Types - Output Types - Resume Index - Last NumParams -} - -C Function[name,inputs,outputs,convention:out] -{ - out <- C Function With Registry[name,inputs,outputs,convention, C Method Registry[], C Field Registry[], C Type Registry[]] -} - -C Function With Registry[name,inputs,outputs,convention,registry,field reg,type reg:out] -{ - out <- [[[[[[[[[[[[[[Build["C Function"] - ]Name <<[name] - ]Inputs <<[inputs] - ]Outputs <<[outputs] - ]Convention <<[convention] - ]Variables <<[Dictionary[]] - ]Statements <<[()] - ]Method Registry <<[registry] - ]Field Registry <<[field reg] - ]Type Registry <<[type reg] - ]Constants <<[Dictionary[]] - ]Input Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), inputs] ] - ]Output Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), outputs] ] - ]Resume Index <<[1] - ]Last NumParams <<[-1] -} - -Set Input Type@C Function[func,type,input num:out] -{ - out <- [func]Input Types <<[ [[func]Input Types >>]Set[input num, type] ] -} - -Set Output Type@C Function[func,type,output num:out] -{ - out <- [func]Output Types <<[ [[func]Output Types >>]Set[output num, type] ] -} - -Register Constant@C Function[func,name,constant:out] -{ - out <- [func]Constants <<[ [[func]Constants >>]Set[name, constant] ] -} - -Allocate Var@C Function[func,name,type:out] -{ - out <- [func]Variables <<[ [[func]Variables >>]Set[name,type] ] -} - -Add Statement@C Function[func,statement:out] -{ - out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[statement]Append[";\n"]]] ] -} - -Add Raw Line@C Function[func,line:out] -{ - out <- [func]Statements <<[ [[func]Statements >>]Append[["\t"]Append[[line]Append["\n"]]] ] -} - -Add Operator Statement@C Function[func,psource1,psource2,pdest,op:out] -{ - source1 <- [psource1]Make Op[func] - source2 <- [psource2]Make Op[func] - dest <- [pdest]Make Op[func] - out <- [func]Add Statement[[[[[dest]Append[" = "]]Append[source1]]Append[op]]Append[source2]] -} - -Add@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," + "] -} - -Sub@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," - "] -} - -Multiply@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," * "] -} - -Divide@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," / "] -} - -DoLShift@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," << "] -} - -DoRShift@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," >> "] -} - -CompLess@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," < "] -} - -CompGreater@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," > "] -} - -CompEqual@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," == "] -} - -CompLessEqual@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," <= "] -} - -CompGreaterEqual@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," >= "] -} - -CompNotEqual@C Function[func,source1,source2,dest:out] -{ - out <- [func]Add Operator Statement[source1,source2,dest," != "] -} - -Move@C Function[func,psource,pdest:out] -{ - source <- [psource]Make Op[func] - dest <- [pdest]Make Op[func] - out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]] -} - -Do AddRef@C Function[func,psource,pdest:out] -{ - source <- [psource]Make Op[func] - dest <- [pdest]Make Op[func] - out <- [func]Add Statement[[[[dest]Append[" = add_ref((object *)"]]Append[source]]Append[")"]] -} - -AddRef No Dest@C Function[func,psource:out] -{ - source <- [psource]Make Op[func] - out <- [func]Add Statement[[["add_ref((object *)"]Append[source]]Append[")"]] -} - -Release@C Function[func,psource:out] -{ - source <- [psource]Make Op[func] - out <- [func]Add Statement[[["release_ref((object *)"]Append[source]]Append[")"]] -} - -Set Null@C Function[func,pdest:out] -{ - dest <- [pdest]Make Op[func] - out <- [func]Add Statement[[dest]Append[" = NULL"]] -} - -Lookup Constant@C Function[func,const,doaddref:out] -{ - var <- ["_const_"]Append[Escape Rhope Name[const]] - If[doaddref] - { - out <- [["add_ref("]Append[var]]Append[")"] - }{ - out <- Val[var] - } -} - -Field Result@C Function[func,var,field:out] -{ - as op <- [var]Make Op[func] - If[[Type Of[var]] = ["String"]] - { - [[func]Inputs >>]Find[var] - { - type <- [[func]Input Types >>]Index[~] - }{ - type <- [[func]Variables >>]Index[var] {} - { - [[func]Outputs >>]Find[var] - { - type <- [[func]Output Types >>]Index[~] - }{ - //Does it make sense for us to do this? - type <- Type Instance["Any Type"] - } - } - } - }{ - type <- Type Instance["Any Type"] - } - If[[[func]Convention >>] = ["rhope"]] - { - If[[type] = ["Any Type"]] - { - rvar <- Val[as op] - }{ - rvar <- [[[["(("]Append[ Rhope Type to C[type] ]]Append[")("]]Append[as op]]Append["))"] - } - }{ - rvar <- Val[as op] - } - - [[func]Type Registry >>]Simple Type?[[type]Name >>] - { access <- "->" } - { access <- "->payload." } - out <- [[rvar]Append[access]]Append[Escape Rhope Name[field]] -} - -Read Field@C Function[func,var,field:out,result op] -{ - out <- func - result op <- Field Ref[var,field] -} - -Write Field@C Function[func,var,field:out,result op] -{ - out <- func - result op <- Field Ref[var,field] -} - -Set Field Null@C Function[func,var,field:out] -{ - out <- [func]Add Statement[ [[func]Field Result[var,field]]Append[" = NULL"] ] -} - -Copy@C Function[func,pdest:out] -{ - dest <- [pdest]Make Op[func] - out <- [func]Add Statement[ [dest]Append[[[" = copy_object("]Append[dest]]Append[")"]] ] -} - -Box@C Function[func,psource,pdest,type:out] -{ - dest <- [pdest]Make Op[func] - source <- [psource]Make Op[func] - out <- [func]Add Statement[ - [[[[[dest - ]Append[" = naked_to_boxed("] - ]Append[ [[func]Type Registry >>]Type ID[[type]Name >>] ] - ]Append[", &"] - ]Append[source] - ]Append[")"] ] -} - -Unbox@C Function[func,psource,pdest:out] -{ - dest <- [pdest]Make Op[func] - source <- [psource]Make Op[func] - out <- [func]Add Statement[ - [[[["boxed_to_naked(" - ]Append[source] - ]Append[", &"] - ]Append[dest] - ]Append[")"] ] -} - -Get Raw Pointer@C Function[func,psource,pdest:out] -{ - dest <- [pdest]Make Op[func] - source <- [psource]Make Op[func] - out <- [func]Add Statement[ [[[dest]Append[" = (void*)("]]Append[source]]Append[" + 1)"] ] -} - -Array Raw Pointer@C Function[func,psource,pdest:out] -{ - dest <- [pdest]Make Op[func] - source <- [psource]Make Op[func] - out <- [func]Add Statement[ [[[dest]Append[" = ((char *)"]]Append[source]]Append[")+ sizeof(t_Array)"] ] -} - -_Function Arg C[func,val,inputnum:out] -{ - out <- [func]Add Raw Line[ - [[[["SetParam(" - ]Append[inputnum] - ]Append[", "] - ]Append[val] - ]Append[")"] - ] -} - -_Val Function Arg C[func,val,inputnum,worker:out] -{ - out <- [func]Add Raw Line[ - [[[[[["VCSetParam(" - ]Append[worker] - ]Append[", "] - ]Append[inputnum] - ]Append[", "] - ]Append[val] - ]Append[")"] - ] -} - -Method Call@C Function[func,method,args:out] -{ - out <- [func]Call[method,args] -} - -Val Call@C Function[func,to call,args:out] -{ - worker <- Make Op[Strip Addref[to call], func] - rargs <- Map[args, ["Make Op"]Set Input[1, func]] - - If[[[func]Last NumParams >>] = [-1]] - { - prepped <- [[func]Add Raw Line[ - [[[["VCPrepCall(" - ]Append[worker] - ]Append[", "] - ]Append[[rargs]Length] - ]Append[")"] ] - ]Last NumParams <<[[rargs]Length] - }{ - prepped <- [[func]Add Raw Line[ - [[[[[["VCRePrepCall(" - ]Append[worker] - ]Append[", "] - ]Append[[rargs]Length] - ]Append[", "] - ]Append[[func]Last NumParams >>] - ]Append[")"] ] - ]Last NumParams <<[[rargs]Length] - } - - out <- [[[[Fold[["_Val Function Arg C"]Set Input[3, worker], prepped, rargs] - ]Add Raw Line[ - [[[[[[[["ValCall(" - ]Append[worker] - ]Append[", "] - ]Append[[rargs]Length] - ]Append[", "] - ]Append[[func]Resume Index >>] - ]Append[", "] - ]Append[Escape Rhope Name[[func]Name >>]] - ]Append[")"]] - ]Add Raw Line["DISPATCH"] - ]Add Raw Line[ - [[[["ValCallPostlude(" - ]Append[[func]Resume Index >>] - ]Append[", "] - ]Append[Escape Rhope Name[[func]Name >>]] - ]Append[")"]] - ]Resume Index <<[ [[func]Resume Index >>]+[1] ] -} - -Call@C Function[func,name,args:out] -{ - If[[name]=["Call@Worker"]] - { - //TODO: Handle case when user explicitly calls the fully qualified version, but the type of the first arg isn't Worker - out <- [func]Val Call[[args]Index[0], Tail[args,1]] - }{ - If[[name]=["Call"]] - { - to call <- [args]Index[0] - last numparams <- [func]Last NumParams >> - out <- [[[[[[func]Add Raw Line[[["if (get_blueprint("]Append[Make Op[Strip Addref[to call], func]]]Append[")->type_id == TYPE_WORKER) {"]] - ]Val Call[to call, Tail[args,1]] - ]Add Raw Line["} else {"] - ]Last NumParams <<[last numparams] - ]Func Base["Call",args, "Call"] - ]Add Raw Line["}"] - }{ - out <- [func]Func Base[Escape Rhope Name[name],args, "Call"] - } - } -} - -Func Base@C Function[func,tocall,args,type:out] -{ - Print[ [[func]Name >>]Append[ [": Func Base("]Append[tocall] ] ] - rargs <- Map[args, ["Make Op"]Set Input[1, func]] - - If[[[rargs]Length] > [[func]Last NumParams >>]] - { - If[[[func]Last NumParams >>] = [-1]] - { - freed <- Val[func] - }{ - freed <- [func]Add Raw Line["FreeCall"] - } - prepped <- [[freed]Add Raw Line[ [["PrepCall("]Append[[rargs]Length]]Append[")"] ] - ]Last NumParams <<[[rargs]Length] - }{ - prepped <- Val[func] - } - - - out <- [[Fold["_Function Arg C", prepped, rargs] - ]Add Raw Line[ - [[[[[[[[[type]Append["("] - ]Append[tocall] - ]Append[", "] - ]Append[[rargs]Length] - ]Append[", "] - ]Append[[func]Resume Index >>] - ]Append[", "] - ]Append[Escape Rhope Name[[func]Name >>]] - ]Append[")"]] - ]Resume Index <<[ [[func]Resume Index >>]+[1] ] -} - -Call Foreign@C Function[func,name,language,args,store result:out] -{ - rargs <- Map[args, ["Make Op"]Set Input[1, func]] - //Assume language = "C" for now - base <- [[[name]Append["("]]Append[ Join[rargs, ", "] ]]Append[")"] - ,do store <- If[[Type Of[store result]]=["String"]] - { - ,do store <- If[[store result]=[""]] - { stmt <- Val[base] } - } - - Val[do store] - { stmt <- [[Make Op[store result, func]]Append[" = "]]Append[base] } - out <- [func]Add Statement[stmt] -} - -Get Field Call@C Function[func,field,source:out] -{ - out <- [func]Func Base[Escape Rhope Name[[field]Append[" >>"]], [()]Append[source], "Call"] -} - -Set Field Call@C Function[func,field,object,value:out] -{ - out <- [func]Func Base[Escape Rhope Name[[field]Append[" <<"]], [[()]Append[object]]Append[value], "Call"] -} - -Tail Method Call@C Function[func,method,args:out] -{ - out <- [func]Func Base[[[func]Method Registry >>]Method ID[method],args, "TMCall"] -} - -Tail Call@C Function[func,name,args:out] -{ - out <- [func]Func Base[Escape Rhope Name[name],args, "TCall"] -} - -Resolve@C Function[func,op:out] -{ - If[[[func]Convention >>] = ["rhope"]] - { - [[func]Inputs >>]Find[op] - { - out <- [["my_cdata->params["]Append[~]]Append[" ]"] - }{ - out <- [[["lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]Append[Escape Rhope Name[op]] - } - }{ - out <- Escape Rhope Name[op] - } -} - -Resolve Output@C Function[func,name:out] -{ - If[[[func]Convention >>] = ["rhope"]] - { - out <- [[["lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]Append[Escape Rhope Name[name]] - }{ - out <- Escape Rhope Name[name] - } -} - -Instruction Stream@C Function[func:out] -{ - out <- [func]Statements <<[()] -} - -_If C[func, statement:out] -{ - out <- [func]Statements <<[ [[func]Statements >>]Append[ ["\t"]Append[statement] ] ] -} - -Do If@C Function[func,condition,stream:out] -{ - cond <- [condition]Make Op[func] - out <- [[Fold["_If C", [[func - ]Add Raw Line[ [["if("]Append[cond]]Append[")"] ] - ]Add Raw Line["{"], [stream]Statements >>] - ]Add Raw Line["}"] - ]Resume Index <<[[stream]Resume Index >>] - -} - -Discard Outputs@C Function[func,first to discard:out] -{ - out <- [[[[[func - ]Add Raw Line[[["for(idx = "]Append[first to discard]]Append["; idx < cdata->num_params; ++idx)"]] - ]Add Raw Line["{"] - ]Add Raw Line[" if (cdata->params[idx])"] - ]Add Raw Line[" release_ref(cdata->params[idx]);"] - ]Add Raw Line["}"] -} - -Result Reference@C Function[func,output:out] -{ - out <- [["cdata->params["]Append[output]]Append["]"] -} - -Checked Result Reference@C Function[func,output:out] -{ - out <- [[[["("]Append[output]]Append[" < cdata->num_params ? cdata->params["]]Append[output]]Append["] : NULL)"] -} - - -If Null Else@C Function[func,left,right:out] -{ - check <- [[Make Condition[left]]Strip Addref]Make Op[func] - l <- [left]Make Op[func] - r <- [right]Make Op[func] - out <- [[[[[["(" - ]Append[check] - ]Append[" ? "] - ]Append[l] - ]Append[" : "] - ]Append[r] - ]Append[")"] -} - -_Set Outputs C[string,inputname,inputnum,func:out] -{ - out <- [string]Append[[[ [ ["\tRet("]Append[inputnum] ]Append[ [[", lv_"]Append[Escape Rhope Name[[func]Name >>]]]Append["->"]]]Append[Escape Rhope Name[inputname]]]Append[")\n"]] -} - -Set Outputs@C Function[func:out] -{ - If[[[func]Convention >>] = ["rhope"]] - { - out <- [[[Fold[["_Set Outputs C"]Set Input[3, func], "", [func]Outputs >>]]Append["\tNumRet("]]Append[[[func]Outputs >>]Length]]Append[")\n"] - }{ - [[func]Outputs >>]Index[0] - { - out <- [["\treturn "]Append[Escape Rhope Name[~]]]Append[";\n"] - }{ - out <- "" - } - } -} -_Output Defs C[string,varname,index,func:out] -{ - out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[[[func]Output Types >>]Index[index]]] ]]Append[[" "]Append[Escape Rhope Name[varname]]]]Append[";\n"] -} -_Var Defs C[string,type,varname:out] -{ - out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type]] ]]Append[[" "]Append[Escape Rhope Name[varname]]]]Append[";\n"] -} - - -Definitions@C Function[func:out] -{ - Print[["Definitions@C Function: "]Append[[func]Name >>]] - { - If[ [[[func]Convention >>] = ["rhope"]] And [[ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] > [0]] ] - { - localtype <- [[[Fold[["_Output Defs C"]Set Input[3, func], Fold["_Var Defs C","typedef struct {\n", [func]Variables >>], [func]Outputs >>]]Append["} lt_"]]Append[Escape Rhope Name[[func]Name >>]]]Append[";\n"] - }{ - localtype <- "" - } - - If[ [[func]Convention >>] = ["rhope"] ] - { - /* parts <- [[func]Name >>]Split["@"] - [parts]Index[1] - { - proto <- [[[["MethodDef(" - ]Append[Escape Rhope Name[[parts]Index[0]]] - ]Append[", "] - ]Append[Escape Rhope Name[~]] - ]Append[")\n"] - }{ - proto <- [["FuncDef("]Append[Escape Rhope Name[[func]Name >>]]]Append[")\n"] - } */ - out <- Val[localtype] - }{ - out <- [[func]Naked Proto]Append[";\n"] - } - } -} - -_Proto Input[list,input,index,types:out] -{ - out <- [list]Append[ [[Rhope Type to C[[types]Index[index]]]Append[" "]]Append[Escape Rhope Name[input]] ] -} - -Naked Proto@C Function[func:out] -{ - [[func]Output Types >>]Index[0] - { - outtype <- [Rhope Type to C[~]]Append[" "] - }{ - outtype <- "void " - } - out <- [[[[outtype - ]Append[ Escape Rhope Name NU[[func]Name >>]] - ]Append["("] - ]Append[ [Fold[["_Proto Input"]Set Input[3, [func]Input Types >>], (), [func]Inputs >>]]Join[", "] ] - ]Append[")"] -} - -Type Check@C Function[func,text,type,input num:out] -{ - If[[type] = ["Any Type"]] - { - out <- text - }{ - out <- [text]Append[ [["\tParam("]Append[input num]]Append[ [[", "]Append[ [[func]Type Registry >>]Type ID[type] ]]Append[")"] ] ] - } -} - -Check Param Type C[text,type,input num,func:out] -{ - If[[Type Of[type]] = ["String"]] - { - typename <- type - }{ - typename <- [type]Name >> - } - If[[typename] = ["Any Type"]] - { - out <- text - }{ - out <- [text]Append[[[["\tParam("]Append[input num]]Append[ [","]Append[ [[func]Type Registry >>]Type ID[typename] ] ]]Append[")\n"]] - } -} - -Text@C Function[func:out] -{ - Print[["Text@C Function: "]Append[[func]Name >>]] - If[ [[func]Convention >>] = ["rhope"] ] - { - ,before <- [[func]Name >>]Get DString["@"] - { - type <- "MethodImpl" - cname <- [[[[Escape Rhope Name[before] - ]Append[", "] - ]Append[Escape Rhope Name[~]] - ]Append[", "] - ]Append[ [[func]Type Registry >>]Type ID[~] ] - }{}{}{ - type <- "Func" - cname <- Val[fname] - } - fname <- Escape Rhope Name[[func]Name >>] - param check <- Fold[["Check Param Type C"]Set Input[3, func], "", [func]Input Types >>] - If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ] - { - out <- [[[[[[[[ [type]Append["NoLocals("] - ]Append[cname] - ]Append[",\n\tNumParams "] - ]Append[ [[func]Inputs >>]Length ] - ]Append[")\n\n"] - ]Append[param check] - ]Append[ [[func]Statements >>]Join[""] ] - ]Append["EndFuncNoLocals\n"] - ]Append["DISPATCH"] - }{ - If[[[func]Last NumParams >>] = [-1]] - { - freecall <- "" - }{ - freecall <- "\n\tFreeCall\n" - } - out <- [[[[[[[[[[ [type]Append["("] - ]Append[cname] - ]Append[",\n\tNumParams "] - ]Append[ [[func]Inputs >>]Length ] - ]Append[")\n\n"] - ]Append[param check] - ]Append[ [[func]Statements >>]Join[""] ] - ]Append[freecall] - ]Append[[func]Set Outputs] - ]Append[[["EndFunc("]Append[fname]]Append[")\n"]] - ]Append["DISPATCH"] - } - }{ - - out <- [[[ - Fold[["_Output Defs C"]Set Input[3, func], - Fold["_Var Defs C", [[func]Naked Proto]Append["\n{"], [func]Variables >>], [func]Outputs >>] - ]Append[[[func]Statements >>]Join[""]] - ]Append[[func]Set Outputs] - ]Append["}"] - } -} - -Blueprint C Program -{ - Functions - Method Registry - Field Registry - Type Registry - Libraries -} - -C Program[:out] -{ - out <- [[[[[Build["C Program"]]Functions <<[Dictionary[]]]Method Registry <<[C Method Registry[]]]Type Registry <<[C Type Registry[]]]Field Registry <<[C Field Registry[]]]Libraries <<[Dictionary[]] -} - -Link@C Program[program,language,library:out] -{ - If[[library] = ["runtime"]] - { - out <- program - }{ - langlibs <- [[program]Libraries >>]Index[language] {} - { langlibs <- Dictionary[] } - out <- [program]Libraries <<[ [[program]Libraries >>]Set[language, [langlibs]Set[library, Yes]] ] - } -} - -Register Type@C Program[program,def:out] -{ - out <- [[[program]Type Registry <<[ [[program]Type Registry >>]Register Type[def] ] - ]Method Registry <<[ [def]Register Methods[[program]Method Registry >>] ] - ]Field Registry <<[ [def]Register Fields[[program]Field Registry >>] ] -} - -Create Type@C Program[program,name:out] -{ - out <- C Type[name] -} - -Create Function@C Program[program,name,inputs,outputs,convention:out] -{ - out <- C Function With Registry[name,inputs,outputs,convention, [program]Method Registry >>, [program]Field Registry >>, [program]Type Registry >>] -} - -Store Function@C Program[program,func:out] -{ - out <- [program]Functions <<[ [[program]Functions >>]Set[ [func]Name >>, func] ] -} - -Method?@C Program[program,funcname:is,isnot] -{ - is,isnot <- [[program]Method Registry >>]Method ID[funcname] -} - -_Defs C Program[text,func:out] -{ - def <- [func]Definitions - If[[def]=[""]] - { - out <- text - }{ - out <- [text]Append[[def]Append["\n\n"]] - } -} - -_Text C Program[text,func,type reg:out] -{ - out <- [text]Append[[[ [func]Type Registry <<[type reg] ]Text]Append["\n\n"]] -} - -Combine Consts[consts,func:out] -{ - out <- Combine[[func]Constants >>, consts] -} - -_Consts C Program[text,value,name:out] -{ - out <- [text]Append[ [["object * _const_"]Append[Escape Rhope Name[name]]]Append[";\n"] ] -} - -_Consts C Release[text,value,name:out] -{ - out <- [text]Append[ [["\trelease_ref(_const_"]Append[Escape Rhope Name[name]]]Append[");\n"] ] -} - -_List Literal El[text,val,index,type reg:out] -{ - out <- [[[[text - ]Append[", "] - ]Append[index] - ]Append[", "] - ]Append[Const Construct C[val, type reg]] -} - -Const Construct C[value,type reg:out] -{ - valtype <- Type Of[value] - [("Int32","Whole Number")]Find[valtype] - { - out <- [["make_Int32("]Append[value]]Append[")"] - }{ - If[[valtype] = ["Type Instance"]] - { - //TODO: Support parametric types - typeid <- [type reg]Type ID[[value]Name >>] - out <- [["make_Blueprint("]Append[typeid]]Append[")"] - }{ - If[[valtype] = ["Yes No"]] - { - If[value] - { - out <- "make_Bool(1)" - }{ - out <- "make_Bool(0)" - } - }{ - If[[valtype] = ["Machine Integer"]] - { - If[[value]Signed? >>] - { s <- "I" } - { s <- "UI" } - - out <- [[[[[["make_" - ]Append[s] - ]Append["nt"] - ]Append[[value]Size >>] - ]Append["("] - ]Append[[value]Value >>] - ]Append[")"] - }{ - If[[valtype] = ["String"]] - { - out <- [["make_String(\""]Append[ [[[[value]Replace["\\", "\\\\"]]Replace["\n", "\\n"]]Replace["\r", "\\r"]]Replace["\"", "\\\""] ]]Append["\")"] - }{ - If[[valtype]=["Worker Literal"]] - { - //TODO: Figure out how to fully support these in nested cases - //or workaround the problem higher up in the food chain - [[value]Args >>]Last - { size <- [~]+[1] } - { size <- "0" } - out <- [[[[[["make_Worker(FUNC_" - ]Append[Escape Rhope Name[[value]Name >>]] - ]Append[", "] - ]Append[size] - ]Append[", "] - ]Append[Fold[["+"]Set Input[0, 1], 0, [value]Args >>]] - ]Append[")"] - }{ - If[[valtype]=["List"]] - { - out <- [Fold[["_List Literal El"]Set Input[3, type reg], ["make_List("]Append[[value]Length], value]]Append[")"] - }{ - out <- "UnhandledLiteralType" - } - } - } - } - } - } - - } -} - -_Set Worker Params C[text,param,num,type reg,name:out] -{ - out <- [text]Append[ - [[[[[["\t((object **)(((t_Worker *)_const_" - ]Append[name] - ]Append[")+1))["] - ]Append[num] - ]Append["] = "] - ]Append[Const Construct C[param, type reg]] - ]Append[";\n"] ] -} - -_Set Consts C Program[text,value,name,type reg:out] -{ - valtype <- Type Of[value] - [("String","Worker Literal","List")]Find[valtype] - { - out <- text - }{ - Const Construct C[value,type reg] - { out <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] } - } -} - -_Set List Els[text,el,index,type reg:out] -{ - out <- [[text]Append[ - [["\tinout[1] = " - ]Append[Const Construct C[index,type reg]] - ]Append[ - [[";\n\tinout[2] = " - ]Append[Const Construct C[el, type reg]] - ]Append[";\n"] - - ]]]Append["\trhope(FUNC_Set, inout, 3, 3);\n"] -} - -_Set Late Consts C[text,value,name,type reg:out] -{ - valtype <- Type Of[value] - [("String","Worker Literal","List")]Find[valtype] - { - If[[valtype]=["List"]] - { - out <- [Fold[["_Set List Els"]Set Input[3, type reg], [text]Append["\trhope(FUNC_List, inout, 0, 1);\n"], value] - ]Append[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = inout[0];\n"]] - }{ - Const Construct C[value,type reg] - { init <- [text]Append[ [[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = "]]Append[~]]Append[";\n"] ] } - - If[[valtype]=["Worker Literal"]] - { - out <- Fold[[["_Set Worker Params C"]Set Input[3, type reg]]Set Input[4, Escape Rhope Name[name]], init, [value]Args >>] - }{ - out <- Val[init] - } - } - }{ - out <- text - } -} - -_Dispatch Switch Sub[text, num, name:out] -{ - out <- [[[[[text - ]Append["\tResumeEntry("] - ]Append[num] - ]Append[","] - ]Append[name] - ]Append[")\\\n"] -} - -_Dispatch Switch[text,func,raw name:out] -{ - If[[[func]Convention >>] = ["rhope"]] - { - name <- Escape Rhope Name[raw name] - out <- [[text]Append[ [["\tDispatchEntry("]Append[name]]Append[")\\\n"] ] - ]Append[Fold[["_Dispatch Switch Sub"]Set Input[2, name], "", Range[1, [func]Resume Index >>]]] - }{ - out <- text - } -} - -_Dispatch Switch Methods[text,id,raw name:out] -{ - name <- Escape Rhope Name[raw name] - out <- [text]Append[ [["\tDispatchEntry("]Append[name]]Append[")\\\n"] ] -} - -_Dispatch Enum Sub[text, num, name:out] -{ - out <- [[[[[text - ]Append["\tRES_"] - ]Append[num] - ]Append["_"] - ]Append[name] - ]Append[",\n"] -} - -_Dispatch Enum[text,func,raw name:out] -{ - If[[[func]Convention >>] = ["rhope"]] - { - name <- Escape Rhope Name[raw name] - out <- [[text]Append[ [["\tFUNC_"]Append[name]]Append[",\n"] ] - ]Append[Fold[["_Dispatch Enum Sub"]Set Input[2, name], "", Range[1, [func]Resume Index >>]]] - }{ - out <- text - } -} - -_Dispatch Enum Methods[text,types,name:out] -{ - out <- [text]Append[ [["\tFUNC_"]Append[Escape Rhope Name[name]]]Append[",\n"] ] -} - -Dispatch@C Program[program,all methods:out] -{ - out <- [[[[["typedef enum {\n" - ]Append[Fold["_Dispatch Enum", - [Fold["_Dispatch Enum Methods", "", all methods]]Append["\tFUNC_Build,\n\tFUNC_BlueprintSP_Of,\n\tFUNC_ID,\n\tFUNC_BlueprintSP_FromSP_ID,\n"], - [program]Functions >>]] - ]Append["\tEND\n} funcids;\n\n"] - ]Append["#define DispatchEntries \\\n"] - ]Append[Fold["_Dispatch Switch", - [Fold["_Dispatch Switch Methods", "", all methods]]Append["\tDispatchEntry(Build)\\\n\tDispatchEntry(BlueprintSP_Of)\\\n\tDispatchEntry(ID)\\\n\tDispatchEntry(BlueprintSP_FromSP_ID)\\\n"], - [program]Functions >>]] - ]Append["\tEndEntry\n\n"] -} - -Not Native[func:out] -{ - If[[[func]Convention >>] = ["rhope"]] - { out <- No } - { out <- Yes } -} - -Native[func:out] -{ - out <- [[func]Convention >>] = ["rhope"] -} - -Local Pointers[text,func:out] -{ - If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ] - { - out <- text - }{ - out <- [text]Append[[["\tFuncDef("]Append[Escape Rhope Name[[func]Name >>]]]Append[")\n"]] - } -} - -_Method to Types[dict,name,type:out] -{ - typelist <- [dict]Index[name] {} - { typelist <- () } - - out <- [dict]Set[name, [typelist]Append[[type]Name >>]] - -} - -_Field to Types[dict,field,type:out] -{ - name <- [field]Index[0] - out <- _Method to Types[_Method to Types[dict, [name]Append[" >>"], type], [name]Append[" <<"], type] - -} - -Method to Types[dict,type:out] -{ - out <- Fold[["_Method to Types"]Set Input[2, type], dict, [type]Methods >>] -} - -Field to Types[dict,type:out] -{ - out <- Fold[["_Field to Types"]Set Input[2, type], dict, [type]Fields >>] -} - -_Method Dispatch[text, type, method, reg: out] -{ - out <- [[[[[[[text]Append["\tMethodDispatch("]]Append[ [reg]Type ID[type] ]]Append[","]]Append[Escape Rhope Name[method]]]Append[","]]Append[Escape Rhope Name[type]]]Append[")\n"] -} - -Method Dispatch[text, types, method, reg: out] -{ - out <- [[[Fold[[["_Method Dispatch"]Set Input[2, method]]Set Input[3, reg], [[[text]Append["Method("]]Append[ Escape Rhope Name[method] ]]Append[")\n"], types] - ]Append["EndMethod("] - ]Append[Escape Rhope Name[method]] - ]Append[")\n\n"] -} - -Init Type Names[text,typeid,name,reg:out] -{ - [reg]Defined?[name] - { out <- [text]Append[ [[[["\tregistered_types["]Append[typeid]]Append["]->name = "]]Append[Const Construct C[name, reg]]]Append[";\n"] ] } - { out <- text } -} - -Text@C Program[program:out] -{ - type defs <- [[program]Type Registry >>]Definitions >> - constants <- Fold["Combine Consts", Dictionary[], [program]Functions >>] - all methods <- Fold["Field to Types", Fold["Method to Types", Dictionary[], type defs], type defs] - headers <- "#include -#include -#include \"builtin.h\" -#include \"object.h\" -#include \"context.h\" -#include \"func.h\" -#include \"integer.h\" -#include \"blueprint.h\" -#include \"array.h\" -#include \"worker.h\" -#include \"bool.h\" -#include \n\n" - out <- [[[[[[[[[[[[[[[[[[headers - ]Append[[program]Dispatch[all methods]] - ]Append[[[program]Type Registry >>]Type Defs] - ]Append[Fold["_Consts C Program", - Fold["_Defs C Program", "", [program]Functions >>], - constants]] - ]Append[Fold[["_Text C Program"]Set Input[2, [program]Type Registry >>], "", Filter[[program]Functions >>, "Not Native"]]] - ]Append["\n -#ifdef ENABLE_PROFILING -uint64_t profile_counts[END]; -uint64_t profile_nestedcounts[END]; -uint64_t profile_totals[END]; -uint64_t profile_selftotals[END]; -uint64_t profile_activationlevel[END]; -#endif - -int32_t rhope(uint32_t func, object ** params, uint16_t numparams, uint16_t callspace) -{ -#ifdef ENABLE_PROFILING - struct timeval time; -#endif - uint16_t resume,idx, vcparam_offset, last_vcparam; - context * ct; - calldata * cdata, *temp_cdata, *my_cdata; - DispatchVar - FuncDef(Build) - FuncDef(BlueprintSP_Of) - FuncDef(ID) - FuncDef(BlueprintSP_FromSP_ID)\n"] - ]Append[Fold["Local Pointers", "", [program]Functions >>]] - ]Append[" - ct = new_context(); - cdata = alloc_cdata(ct, NULL, callspace); - cdata->num_params = numparams; - for(idx = 0; idx < numparams; ++idx) - cdata->params[idx] = params[idx]; - cdata->func = END; -DISPATCH\n"] - ]Append[Fold[["Method Dispatch"]Set Input[3, [program]Type Registry >>], "", all methods]] - ]Append[" -Func(Build, - NumParams 1) - - Param(0, TYPE_BLUEPRINT) - - lv_Build->bp = ((t_Blueprint *)(cdata->params[0]))->bp; - release_ref(cdata->params[0]); - - Ret(0, new_object_bp(lv_Build->bp)) -EndFunc(Build) -DISPATCH - -Func(BlueprintSP_Of, - NumParams 1) - - lv_BlueprintSP_Of->bp = get_blueprint(cdata->params[0]); - release_ref(cdata->params[0]); - - Ret(0, new_object(TYPE_BLUEPRINT)) - ((t_Blueprint *)cdata->params[0])->bp = lv_BlueprintSP_Of->bp; -EndFunc(BlueprintSP_Of) -DISPATCH - -Func(ID, NumParams 1) - - Param(0, TYPE_BLUEPRINT) - - lv_ID->id = new_object(TYPE_UINT32); - ((t_UInt32 *)lv_ID->id)->Num = ((t_Blueprint *)cdata->params[0])->bp->type_id; - release_ref(cdata->params[0]); - Ret(0, lv_ID->id) -EndFunc(ID) -DISPATCH - -Func(BlueprintSP_FromSP_ID, NumParams 1) - - Param(0, TYPE_UINT32) - - lv_BlueprintSP_FromSP_ID->type = ((t_UInt32 *)cdata->params[0])->Num; - if (lv_BlueprintSP_FromSP_ID->type >= max_registered_type || !registered_types[lv_BlueprintSP_FromSP_ID->type]) { - Ret(1, cdata->params[0]) - Ret(0, NULL) - } else { - release_ref(cdata->params[0]); - Ret(0, new_object(TYPE_BLUEPRINT)) - ((t_Blueprint *)cdata->params[0])->bp = registered_types[lv_BlueprintSP_FromSP_ID->type]; - Ret(1, NULL) - } - -EndFunc(BlueprintSP_FromSP_ID) -DISPATCH\n"] - ]Append[Fold[["_Text C Program"]Set Input[2, [program]Type Registry >>], "", Filter[[program]Functions >>, "Native"]]] - ]Append[" -DO_END: - for(idx = 0; idx < cdata->num_params; ++idx) - params[idx] = cdata->params[idx]; - free_context(ct); - return cdata->num_params; - -_exception: - puts(\"Exception! Trace follows:\"); - while(cdata && cdata->func != END) - { - printf(\"%d\\n\", cdata->func); - cdata = cdata->lastframe; - } - return -1; -} - -#include \"builtin.c\" -#include \"array.c\" -#include \"worker.c\" - -int main(int argc, char **argv) -{ - blueprint * bp; - int numret,retval; - int idx,sidx; - object * inout[3]; - int32_t *counts, string_type_id=0; - t_String ** live_strings; - char ** typenames; - t_Array * typename; - register_builtin_types();\n\n"] - ]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ] - ]Append[Fold[["_Set Consts C Program"]Set Input[3, [program]Type Registry >>], "", constants]] - ]Append[Fold[["_Set Late Consts C"]Set Input[3, [program]Type Registry >>], "", constants]] - ]Append[Fold[["Init Type Names"]Set Input[3, [program]Type Registry >>], "", [[program]Type Registry >>]Lookup >>]] - ]Append[" - rhope(FUNC_List, inout, 0, 1); - for (idx = 0; idx < argc; ++idx) - { - inout[1] = make_String(argv[idx]); - rhope(FUNC_Append, inout, 2, 2); - } - numret = rhope(FUNC_Main, inout, 1, 1);"] - ]Append[Fold["_Consts C Release", "", constants]] - ]Append[ - " - if (!numret) - retval = 0; - else if (numret < 0) - retval = numret; - else if (get_blueprint(inout[0])->type_id == TYPE_INT32) - { - retval = ((t_Int32 *)inout[0])->Num; - release_ref(inout[0]); - } else { - rhope(FUNC_If, inout, 1, 2); - if (inout[0]) - { - retval = 0; - release_ref(inout[0]); - } else { - retval = 1; - release_ref(inout[1]); - } - } - typenames = malloc(sizeof(char *)*max_registered_type); - for(idx = 0; idx < max_registered_type; ++idx) - { - if(registered_types[idx]) - { - typename = (t_Array *)( ((t_String *)registered_types[idx]->name)->payload.Buffer ); - typenames[idx] = malloc(typename->payload.Length+1); - memcpy(typenames[idx], typename+1, typename->payload.Length); - typenames[idx][typename->payload.Length] = 0; - if(!strcmp(typenames[idx], \"String\")) - string_type_id = idx; - release_ref(registered_types[idx]->name); - } else { - typenames[idx] = NULL; - } - } - - print_mem_info(manager); - counts = malloc(sizeof(int32_t)*max_registered_type); - get_live_object_counts(manager, counts); - for(idx = 0; idx < max_registered_type; ++idx) - { - if(counts[idx]) - { - printf(\"%d objects of type %s (%d)\\n\", counts[idx], typenames[idx], idx); - if(idx == string_type_id) - { - live_strings = malloc(sizeof(t_String *)*counts[idx]); - find_live_objects_oftype(manager, string_type_id, (void **)live_strings); - for(sidx = 0; sidx < counts[idx]; ++sidx) - { - printf(\"\t%d: \", sidx); - fwrite(((t_Array *)live_strings[sidx]->payload.Buffer)+1, 1, ((t_Array *)live_strings[sidx]->payload.Buffer)->payload.Length, stdout); - puts(\"\"); - } - } - } - } - fflush(stdout); - -#ifdef ENABLE_PROFILING - for (idx = 0; idx < END; ++idx) - { - if(profile_counts[idx]) - printf(\"Func: %d\tCount: %llu\tTime: %llu\tAvg: %f\tSelf: %llu\tAvg: %f\tNested Count: %llu\\n\", idx, profile_counts[idx], profile_totals[idx], ((double)profile_totals[idx])/((double)profile_counts[idx]), profile_selftotals[idx], ((double)profile_selftotals[idx])/((double)profile_counts[idx]), profile_nestedcounts[idx]); - } -#endif - return retval; -}\n\n"] - -} - - diff -r db30bda91aad -r 24c6f8767190 compile_old.rhope --- a/compile_old.rhope Wed Jul 27 21:06:57 2011 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -Import parser_old.rhope - -Main[args] -{ - fname <- [args]Index[1] - { - file <- >, ["Check Worker Literals"]Set Input[1, ~]] ] - { Print["Compiling"] } - } - compiled <- [Tree to Program Native[tree]]Compile Program[C Program[]] - { Print["Compiled program to backend"] } - outfile <- ", "CompGreater"), ("=", "CompEqual"), ("<=", "CompLessEqual"), (">=", "CompGreaterEqual"), ("!=", "CompNotEqual")) - - Fold[["Compile Number Method"]Set Input[2, type], backend, opmap] - { Fold[["Compile Number Comp Method"]Set Input[2, type], ~, compops] - { Fold[[["Compile Conversion Method"]Set Input[1, type]]Set Input[3, ""], ~, Legal Conversions[type]] - { - almost <- Fold[[["Compile Conversion Method"]Set Input[1, type]]Set Input[3, "Trunc "], ~, Truncations[type]] - If[[type]Starts With["I"]] - { - out <- Compile Abs UInt Method[almost,type] - }{ - out <- Val[almost] - } - }}} -} - -Generate Number Methods[backend:out] -{ - numtypes <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64") - - out <- Fold["_Generate Number Methods", backend, numtypes] -} - -Register Number Method[program, method, type, outtype:out] -{ - name <- [[method]Append["@"]]Append[type] - out <- [[program]Register Worker[name, "rhope", 2, 1] - ]Bind Worker[name, - [[[[[NWorker["rhope"] - ]Inputs <<[("left","right")] - ]Input Types <<[ [[()]Append[ Type Instance[type]]]Append[Type Instance[type]] ] - ]Outputs <<[("out")] - ]Output Types <<[ [()]Append[Type Instance[outtype]] ] - ]Builtin? <<[Yes] - ] -} - -Register Conversion Method[program, intype, outtype,prefix:out] -{ - name <- [prefix]Append[[[outtype]Append["@"]]Append[intype]] - out <- [[program]Register Worker[name, "rhope", 1, 1] - ]Bind Worker[name, - [[[[[NWorker["rhope"] - ]Inputs <<[("in")] - ]Input Types <<[ [()]Append[Type Instance[intype]] ] - ]Outputs <<[("out")] - ]Output Types <<[ [()]Append[Type Instance[outtype]] ] - ]Builtin? <<[Yes] - ] -} - -Legal Conversions[type:convs] -{ - bigger <- Filter[(16,32,64), [">"]Set Input[1, size]] - base convs <- Map[bigger, ["Append"]Set Input[0, "Int"]] - If[[type]Starts With["U"]] - { - [type]Slice[4] {} - { size <- ", "=", "<=", ">=", "!=") - register <- ["Register Number Method"]Set Input[2, type] - Fold[[register]Set Input[3, type], program, methods] - { Fold[[register]Set Input[3, "Boolean"], ~, compmethods] - { Fold[[["Register Conversion Method"]Set Input[1, type]]Set Input[3, ""], ~, Legal Conversions[type]] - { - almost <- Fold[[["Register Conversion Method"]Set Input[1, type]]Set Input[3, "Trunc "], ~, Truncations[type]] - If[[type]Starts With["I"]] - { - name <- ["Abs UInt@"]Append[type] - out <- [[almost]Register Worker[name, "rhope", 1, 1] - ]Bind Worker[name - [[[[[NWorker["rhope"] - ]Inputs <<[("in")] - ]Input Types <<[ [()]Append[Type Instance[type]] ] - ]Outputs <<[("out")] - ]Output Types <<[ [()]Append[Type Instance[["U"]Append[type]]] ] - ]Builtin? <<[Yes] - ] - }{ - out <- Val[almost] - } - }}} -} - -Register Number Methods[program:out] -{ - numtypes <- ("Int8","Int16","Int32","Int64","UInt8","UInt16","UInt32","UInt64") - out <- Fold["_Register Number Methods", program, numtypes] -} - diff -r db30bda91aad -r 24c6f8767190 nworker.rhope --- a/nworker.rhope Wed Jul 27 21:06:57 2011 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1644 +0,0 @@ -Import extendlib.rhope -Import cbackend.rhope -Import number.rhope -Import boolean.rhope - -Blueprint Condition Set -{ - Variables - Subsets - Condition Type -} - -AndSet[:out] -{ - out <- [[[Build["Condition Set"]]Variables <<[Dictionary[]]]Subsets <<[Dictionary[]]]Condition Type <<["And"] -} - -OrSet[:out] -{ - out <- [[[Build["Condition Set"]]Variables <<[Dictionary[]]]Subsets <<[Dictionary[]]]Condition Type <<["Or"] -} - -To String@Condition Set[set:out] -{ - out <- [[[[[set]Condition Type >> - ]Append["Set:\n\tVariables:\n\t\t"] - ]Append[ Join[Keys[[set]Variables >>], "\n\t\t"] ] - ]Append["\n\tSubsets:\n\t\t"] - ]Append[ Join[Keys[[set]Subsets >>], "\n\t\t"] ] -} - -Add Condition@Condition Set[set,cond:out] -{ - If[[Type Of[cond]] = ["Condition Set"]] - { - out <- [set]Subsets <<[ [[set]Subsets>>]Set[[cond]To String, cond] ] - }{ - out <- [set]Variables <<[ [[set]Variables >>]Set[cond, Yes] ] - } -} - -=@Condition Set[set1,set2:out] -{ - ,out <- If[[[set1]Condition Type >>] = [[set2]Condition Type >>]] - { - ,out <- If[[[set1]Variables >>] = [[set2]Variables >>]] - { - out,out <- If[[[set1]Subsets >>] = [[set2]Subsets >>]] - } - } -} - -_For Backend Var[current,junk,variable,type:out] -{ - out <- [[>]First - { - not empty <- Yes - }{ - ,empty <- [[set]Subsets >>]First Non-empty Set - { - not empty <- Yes - } - } -} - -_First Non-empty Set[setlist,index:out,none] -{ - current <- [setlist]Index[index] - [[current]Variables >>]First - { - out <- index - }{ - ,trynext <- [[current]Subsets >>]First Non-empty Set - { - out <- index - } - } - Val[trynext] - { - ,none <- [setlist]Next[index] - { - out,none <- _First Non-empty Set[setlist, ~] - } - } -} - -First Non-empty Set[setlist:index,none] -{ - ,none <- [setlist]First - { - index,none <- _First Non-empty Set[setlist,~] - } -} - -For Backend@Condition Set[set:out,none] -{ - firstvar <- [[set]Variables >>]First - { - [[set]Variables >>]Next[~] - { - vars <- _Fold[[set]Variables >>, ~, firstvar, ["_For Backend Var"]Set Input[3, [set]Condition Type >>]] - }{ - vars <- Val[firstvar] - } - out <- Fold[["_For Backend Subset"]Set Input[2, [set]Condition Type >>], vars, [set]Subsets >>] - }{ - [[set]Subsets >>]First Non-empty Set - { - firstsub <- [[[set]Subsets >>]Index[~]]For Backend - [[set]Subsets >>]Next[~] - { - out <- _Fold[[set]Subsets >>, ~, firstsub, ["_For Backend Subset"]Set Input[2, [set]Condition Type >>]] - }{ - out <- Val[firstsub] - } - }{ - none <- Yes - } - } -} - -Set@Range[range,index,val:out] -{ - out <- [[()]Concatenate[range] - ]Set[index,val] -} - -List of Lists[num:out] -{ - out <- Fold[["Append"]Set Input[1, ()],(), Range[0,num]] -} - -Blueprint Worker Ref -{ - Name - Convention - Inputs - Min Inputs - Outputs - Min Outputs - Is Method? -} - -Worker Ref[name,convention,inputs,outputs,ismethod?:out] -{ - out <- [[[[[[[Build["Worker Ref"]]Name <<[name]]Convention <<[convention]]Inputs <<[inputs]]Outputs <<[outputs]]Is Method? <<[ismethod?]]Min Inputs <<[inputs]]Min Outputs <<[outputs] -} - -Blueprint Node Ref -{ - Index - IO Num -} - -Node Ref[index,ionum:out] -{ - out <- [[Build["Node Ref"]]Index <<[index]]IO Num <<[ionum] -} - -=@Node Ref[left,right:out] -{ - ,out <- If[[[left]Index >>] = [[right]Index >>]] - { - out <- [[left]IO Num>>] = [[right]IO Num >>] - } -} - -Blueprint NWorker Node -{ - Type - Data - Inputs - Min Inputs - Input Types - Outputs - Min Outputs - Output Types - Wires From - Wires To - Conditions -} - -Wire To@NWorker Node[node,from,output,pre input:out] -{ - existing cons <- [[node]Wires To >>]Index[input] {} - { existing cons <- () } - input <- [pre input]+[1] - out <- [node]Wires To <<[ - [[node]Wires To >>]Set[input, - [existing cons]Append[Node Ref[from,output]] - ] - ] -} - -Wire From@NWorker Node[node,to,input,output:out] -{ - existing cons <- [[node]Wires From >>]Index[output] {} - { exist cons <- () } - out <- [node]Wires From <<[ - [[node]Wires From >>]Set[output, - [existing cons]Append[Node Ref[to,input]] - ] - ] -} - -_Has Input Types@NWorker Node[node,input num:does,does not] -{ - does <- If[[input num] > [[node]Inputs >>]] {} - { - ,does not <- [[node]Input Types >>]Index[input num] - { - count <- [~]Index[1] - ,does not <- If[[count] = [[[[node]Wires To >>]Index[input num]]Length]] - { - does,does not <- [node]_Has Input Types[[input num]+[1]] - } - } - } -} - -Has Input Types?@NWorker Node[node:does,does not] -{ - If[[[[node]Inputs >>]Length] > [0]] - { - does,does not <- _Has Input Types[node,0] - }{ - does <- Yes - } -} - -_Dependency[dlist,ref:out] -{ - [dlist]Find[ref] - { - out <- dlist - }{ - out <- [dlist]Append[ref] - } -} - -Dependencies@NWorker Node[node:out] -{ - out <- Fold[["Fold"]Set Input[0, "_Dependency"], (), [node]Wires To >>] -} - - -NWorker Node[type,data,inputs,outputs:out] -{ - out <- [[[[[[[[[[[Build["NWorker Node"] - ]Type <<[type] - ]Data <<[data] - ]Inputs <<[inputs] - ]Min Inputs <<[inputs] - ]Outputs <<[outputs] - ]Min Outputs <<[outputs] - ]Wires From <<[List of Lists[outputs]] - ]Wires To <<[List of Lists[[inputs]+[1]]] - ]Conditions <<[AndSet[]] - ]Input Types <<[()] - ]Output Types <<[()] -} - -Blueprint NWorker -{ - Convention - Nodes - Inputs - Input Types - Outputs - Output Types - Uses - NodeResults - Free Temps - Name - Builtin? - Library -} - -NWorker[convention:out] -{ - out <- [[[[[[[[[Build["NWorker"]]Convention <<[convention]]Nodes <<[()]]Inputs <<[()]]Outputs <<[()]]Input Types <<[()]]Output Types <<[()]]Name <<["Anonymous"]]Builtin? <<[No]]Library << [""] -} - -Add Node@NWorker[worker,type,data,inputs,outputs:out,node index] -{ - out <- [worker]Nodes <<[[[worker]Nodes >>]Append[NWorker Node[type,data,inputs,outputs]]] - node index <- [[worker]Nodes >>]Length -} - -Add Full Node@NWorker[worker,type,data,inputs,min inputs,outputs,min outputs:out,node index] -{ - out <- [worker]Nodes <<[[[worker]Nodes >>]Append[ - [[[NWorker Node[type,data,inputs,outputs] - ]Min Inputs <<[min inputs] - ]Min Outputs <<[min outputs] - ]Wires To <<[List of Lists[[min inputs]+[1]]] - ]] - node index <- [[worker]Nodes >>]Length -} - -Propagate Type[nodelist,dest,prog,worker,type:out] -{ - node <- [nodelist]Index[[dest]Index >>] - - [[node]Input Types >>]Index[[dest]IO Num >>] - { - existing type <- [~]Index[0] - new count <- [[~]Index[1]]+[1] - If[[[existing type]Name >>] = [[type]Name >>]] - { - If[[[existing type]Variant >>] = [[type]Variant >>]] - { - If[[[existing type]Params >>] = [[type]Params >>]] - { - new type <- Val[existing type] - }{ - new variant <- [existing type]Variant >> - new params <- () - } - }{ - new variant <- "Boxed" - If[[[existing type]Params >>] = [[type]Params >>]] - { - new params <- [existing type]Params >> - }{ - new params <- () - } - } - new type <- [[existing type]Set Variant[new variant]]Params <<[new params] - }{ - new type <- Type Instance["Any Type"] - } - }{ - new type <- Val[type] - new count <- 1 - } - new node <- [node]Input Types <<[ - [ [node]Input Types >> ]Set[ [dest]IO Num >>, [[()]Append[new type]]Append[new count] ] - ] - out <- Infer Types Node[[nodelist]Set[[dest]Index >>, new node], new node, [dest]Index >>, prog, worker] -} - -Propagate Types[nodelist,dests,output num,prog,worker,source node:out] -{ - out <- Fold[[[["Propagate Type"]Set Input[2, prog]]Set Input[3, worker]]Set Input[4, [[source node]Output Types >>]Index[output num]], nodelist, dests] -} - -Infer Types Node[nodelist,node,index,prog,worker:out] -{ - If[[[node]Type >>] = ["const"]] - { - const type <- Type Of[[node]Data >>] - //Temporary hack - If[[const type] = ["Whole Number"]] - { - outtype <- Type Instance["Int32"] - }{ - If[[const type] = ["Type Instance"]] - { - outtype <- Type Instance["Blueprint"] - }{ - If[[const type] = ["Machine Integer"]] - { - If[[[node]Data >>]Signed? >>] - { base <- "Int" } - { base <- "UInt" } - - outtype <- Type Instance[[base]Append[ [[node]Data >>]Size >> ]] - }{ - If[[const type] = ["Worker Literal"]] - { - outtype <- Type Instance["Worker"] - }{ - outtype <- Type Instance[const type] - } - } - } - } - nextnode <- [node]Output Types <<[ [()]Append[outtype] ] - - }{ - If[[[node]Type >>] = ["input"]] - { - nextnode <- [node]Output Types <<[ [()]Append[ [[worker]Input Types >>]Index[[node]Data >>] ] ] - }{ - If[[[node]Type >>] = ["output"]] - { - out <- nodelist - - }{ - [node]Has Input Types? - { - If[[[node]Type >>] = ["setfield"]] - { - nextnode <- [node]Output Types <<[ [()]Append[ [[[node]Input Types >>]Index[0]]Index[0] ] ] - }{ - If[[[node]Type >>] = ["getfield"]] - { - type <- [[[node]Input Types >>]Index[0]]Index[0] - If[[[type]Name >>] = ["Any Type"]] - { - outtype <- Val[type] - }{ - outtype <- [prog]Find Field[[node]Data >>, type] {} - { - //TODO: Return errors rather than printing them - Print[ - [[[[["Type " - ]Append[[type]Name >>] - ]Append[" does not have a field named "] - ]Append[[node]Data >>] - ]Append[" in worker "] - ]Append[worker name]] - } - } - nextnode <- [node]Output Types <<[ [()]Append[outtype] ] - }{ - worker name <- [[node]Data >>]Name >> - [prog]Is Method?[worker name] - { - first arg type <- [[[node]Input Types >>]Index[0]]Index[0] - If[[[first arg type]Name >>] = ["Any Type"]] - { - outtypes <- Fold[["Append"]Set Input[1, Type Instance["Any Type"]], (), Range[0, [node]Inputs >>]] - }{ - worker def <- [prog]Find Method[worker name, first arg type] {} - { - //TODO: Return errors instead of printing them - Print[ - [[[[["Type " - ]Append[[first arg type]Name >>] - ]Append[" does not support method "] - ]Append[worker name] - ]Append[" in worker "] - ]Append[ [worker]Name >> ]] - } - } - }{ - worker def <- [prog]Find Worker Def[worker name] - } - outtypes <- [worker def]Output Types >> - nextnode <- [node]Output Types <<[ outtypes ] - } - } - }{ - out <- nodelist - } - } - } - } - - Val[nextnode] - { - nextlist <- [nodelist]Set[index, nextnode] - out <- Fold[[[["Propagate Types"]Set Input[3, prog]]Set Input[4, worker]]Set Input[5, nextnode], nodelist, [nextnode]Wires From >>] - } -} - -Infer Types@NWorker[worker,prog:out] -{ - out <- [worker]Nodes <<[Fold[[["Infer Types Node"]Set Input[3, prog]]Set Input[4, worker], [worker]Nodes >>, [worker]Nodes >>]] -} - -Add Worker Call@NWorker[worker,tocall:out,node index] -{ - out, node index <- [worker]Add Full Node["call",tocall,[tocall]Inputs >>, [tocall]Min Inputs >>,[tocall]Outputs >>, [tocall]Min Outputs >>] -} - -Add Constant@NWorker[worker,constant:out,node index] -{ - out, node index <- [worker]Add Node["const",constant,0,1] -} - -Add Input@NWorker[worker,name,number:out,node index] -{ - out,node index <- [worker]Add Typed Input[name,number,Type Instance["Any Type"]] -} - -Add Anon Input@NWorker[worker,number:out] -{ - If[[number]>[Length[[worker]Inputs >>]]] - { - prepped <- [worker]Add Anon Input[[number]-[1]] - }{ - prepped <- Val[worker] - } - out <- out <- [[prepped]Inputs <<[[[prepped]Inputs >>]Set[number,[" unnamed"]Append[number]]] - ]Input Types <<[[[prepped]Input Types >>]Set[number,Type Instance["Any Type"]]] -} - -Add Typed Input@NWorker[worker,name,number,type:out,node index] -{ - If[[number]>[Length[[worker]Inputs >>]]] - { - prepped <- [worker]Add Anon Input[[number]-[1]] - }{ - prepped <- Val[worker] - } - ,node index <- [prepped]Add Node["input",number,0,1] - { - out <- [[~]Inputs <<[[[~]Inputs >>]Set[number,name]] - ]Input Types <<[[[~]Input Types >>]Set[number,type]] - } -} - -Add Output@NWorker[worker,name,number:out,node index] -{ - out,node index <- [worker]Add Typed Output[name,number,Type Instance["Any Type"]] -} - -Add Typed Output@NWorker[worker,name,number,type:out,node index] -{ - ,node index <- [worker]Add Node["output",number,1,0] - { - out <- [[~]Outputs <<[[[~]Outputs >>]Set[number,name]] - ]Output Types <<[[[~]Output Types >>]Set[number,type]] - } -} - -Add Object Get@NWorker[worker,fieldname:out,node index] -{ - out, node index <- [worker]Add Node["getfield",fieldname,1,1] -} - -Add Object Set@NWorker[worker,fieldname:out,node index] -{ - out, node index <- [worker]Add Node["setfield",fieldname,2,1] -} - -Add Wire@NWorker[worker,from,output,to,input:out] -{ - fromw <- [[[worker]Nodes >>]Index[from]]Wire From[to,input,output] - tow <- [[[worker]Nodes >>]Index[to]]Wire To[from,output,input] - nodes <- [[[worker]Nodes >>]Set[from, fromw]]Set[to, tow] - out <- [worker]Nodes <<[nodes] -} - -Uses@NWorker[worker,uses:out] -{ - out <- [worker]Uses <<[uses] -} - -_No Dependencies[list,node,index:out] -{ - [[node]Wires To>>]Index[1] - { - out <- Val[list] - }{ - [[[node]Wires To>>]Index[0]]First - { - out <- Val[list] - }{ - out <- [list]Append[index] - } - } -} - -No Dependencies@NWorker[worker:out] -{ - out <- Fold["_No Dependencies", (), [worker]Nodes >>] -} - -_Collect Dests[candidates,wire:out] -{ - out <- [candidates]Set[[wire]Index >>, Yes] -} - -Collect Dests@NWorker[worker,candidates,node index:out] -{ - out <- Fold[["Fold"]Set Input[0, "_Collect Dests"], candidates, [[[worker]Nodes >>]Index[node index]]Wires From >>] -} - -Check Dependency@NWorker[worker,nodes,wires,wire index:met?] -{ - ref <- [wires]Index[wire index] - [nodes]Find[[ref]Index >>] - { - [wires]Next[wire index] - { - met? <- [worker]Check Dependency[nodes,wires,~] - }{ - met? <- Yes - } - }{ - met? <- No - } -} -_Check Dependencies@NWorker[worker,nodes,inputs,input index:met?] -{ - wires <- [inputs]Index[input index] - [wires]First - { - current met? <- [worker]Check Dependency[nodes, wires, ~] - }{ - current met? <- Yes - } - If[current met?] - { - [inputs]Next[input index] - { - met? <- [worker]_Check Dependencies[nodes,inputs,~] - }{ - met? <- Yes - } - }{ - met? <- No - } -} - -Check Dependencies@NWorker[worker,nodes,candidate:met?] -{ - inputs <- [[[worker]Nodes >>]Index[candidate]]Wires To >> - [inputs]First - { - met? <- [worker]_Check Dependencies[nodes, inputs, ~] - }{ - met? <- Yes - } -} - -Dependants@NWorker[worker,direct nodes,nodes:out] -{ - candidates <- Keys[Fold[["Collect Dests"]Set Input[0,worker], (), direct nodes]] - out <- Filter[candidates, [["Check Dependencies"]Set Input[0, worker]]Set Input[1, nodes]] -} - -_Dependency Groups@NWorker[worker,last,all,grouped:out] -{ - current <- [worker]Dependants[last,all] - [current]First - { - out <- [worker]_Dependency Groups[current, [all]Concatenate[current], [grouped]Append[current]] - }{ - out <- grouped - } -} -Dependency Groups@NWorker[worker:out] -{ - no deps <- [worker]No Dependencies - out <- [worker]_Dependency Groups[no deps, no deps, [()]Append[no deps]] -} - -Const Name[val,node index,worker name:out] -{ - If[[Type Of[val]] = ["Type Instance"]] - { - //TODO: Support parametric types - datstring <- [val]Name >> - }{ - If[[Type Of[val]] = ["Machine Integer"]] - { - If[[val]Signed? >>] - { s <- "i" } - { s <- "u" } - datstring <- [[[val]Value >>]Append[s]]Append[[val]Size >>] - }{ - If[[Type Of[val]] = ["Worker Literal"]] - { - If[[[[val]Args >>]Length] > [0]] - { - datstring <- [[["Arg "]Append[node index]]Append[" "]]Append[worker name] - }{ - datstring <- [val]Name >> - } - }{ - If[[Type Of[val]] = ["List"]] - { - If[[[val]Length] > [0]] - { - datstring <- [[["Arg "]Append[node index]]Append[" "]]Append[worker name] - }{ - datstring <- "Empty" - } - }{ - datstring <- val - } - } - } - } - out <- [[Type Of[val]]Append["_"]]Append[datstring] -} - -Format Input@NWorker[worker,noderef:out] -{ - node <- [[worker]Nodes >>]Index[[noderef]Index >>] - - [("call","getfield","setfield")]Find[[node]Type >>] - { - maybe addref <- [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] - }{ - conditions <- [node]Conditions >> - - If[[[node]Type >>] = ["input"]] - { - input name <- [[worker]Inputs >>]Index[ [node]Data >> ] - [conditions]For Backend - { - out <- AddRef[ [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] ] - }{ - out <- AddRef[input name] - } - }{ - If[[[node]Type >>] = ["const"]] - { - [conditions]For Backend - { - out <- AddRef[ [[["__result_"]Append[[noderef]Index >>]]Append["_"]]Append[[noderef]IO Num >>] ] - }{ - out <- Constant[Const Name[[node]Data >>, [noderef]Index >>, [worker]Name >>]] - } - } - } - } - - Val[maybe addref] - { - If[[Length[[[node]Wires From >>]Index[[noderef]IO Num >>]]] > [1]] - { - out <- AddRef[maybe addref] - }{ - out <- Val[maybe addref] - } - } -} - -Collect Input@NWorker[worker,nodeinput:out] -{ - inputchoices <- Map[nodeinput, ["Format Input"]Set Input[0, worker]] - - [inputchoices]First - { - first <- [inputchoices]Index[~] - [inputchoices]Next[~] - { - out <- _Fold[inputchoices, ~, first, "OrValue"] - }{ - out <- Val[first] - } - }{ - out <- "Missing" - } -} - -Collect Inputs@NWorker[worker,node:out] -{ - out <- Map[Tail[[node]Wires To>>, 1], ["Collect Input"]Set Input[0, worker]] -} - -Collect Input Condition@NWorker[worker,set,noderef:out] -{ - node <- [[worker]Nodes >>]Index[ [noderef]Index >> ] - If[[[node]Outputs >>] > [1]] - { - out <- [set]Add Condition[ [["__result_"]Append[[noderef]Index >>]]Append[["_"]Append[[noderef]IO Num >>]] ] - }{ - out <- [set]Add Condition[[node]Conditions >>] - } -} - -Collect Condition@NWorker[worker,set,nodeinput:out] -{ - out <- [set]Add Condition[Fold[["Collect Input Condition"]Set Input[0, worker], OrSet[], nodeinput]] -} - -Collect Conditions@NWorker[worker,node:out] -{ - out <- Fold[["Collect Condition"]Set Input[0, worker], AndSet[], [node]Wires To>>] -} - -Save Result[func,num,node index:out] -{ - out var <- [[["__result_"]Append[node index]]Append["_"]]Append[num] - /*out <- [[func]Allocate Var[out var, "Any Type"] - ]Move[Result[num], out var] - */ - out <- [func]Move[Result[num], out var] -} - -Save Maybe Result[func,num,node index:out] -{ - out var <- [[["__result_"]Append[node index]]Append["_"]]Append[num] - out <- [func]Move[Check Result[num], out var] -} - -Max Used Output[node,cur:out] -{ - If[[cur] < [0]] - { - out <- cur - }{ - [[[node]Wires From >>]Index[cur]]Index[0] - { - out <- cur - }{ - out <- Max Used Output[node, [cur]-[1]] - } - } -} - -Compile Call Node[node,program,func,inputs,node index:out] -{ - If[[[node]Type >>] = ["getfield"]] - { - with call <- [func]Get Field Call[[node]Data >>, [inputs]Index[0]] - save outs <- [node]Outputs >> - out <- Val[after save] - }{ - If[[[node]Type >>] = ["setfield"]] - { - with call <- [func]Set Field Call[[node]Data >>, [inputs]Index[0], [inputs]Index[1]] - save outs <- [node]Outputs >> - out <- Val[after save] - }{ - [program]Method?[[[node]Data >>]Name >>] - { - with call <- [func]Method Call[[[node]Data >>]Name >>, inputs] - }{ - with call <- [func]Call[[[node]Data >>]Name >>, inputs] - } - first unused <- [Max Used Output[node, [[node]Outputs >>]-[1]]]+[1] - If[[first unused] > [[node]Min Outputs >>]] - { - save outs <- [node]Min Outputs >> - after maybe <- Fold[["Save Maybe Result"]Set Input[2, node index], after save, Range[save outs, first unused]] - }{ - save outs <- Val[first unused] - after maybe <- Val[after save] - } - If[[first unused] < [[node]Outputs >>]] - { - out <- [after maybe]Discard Outputs[first unused] - }{ - out <- Val[after maybe] - } - } - } - after save <- Fold[["Save Result"]Set Input[2, node index], with call, Range[0, save outs]] -} - -Compile Node@NWorker[worker,program,func,nodes,current:out,out worker] -{ - node index <- [nodes]Index[current] - node <- [[worker]Nodes >>]Index[node index] - conditions <- [node]Conditions >> - [("call","getfield","setfield")]Find[[node]Type >>] - { - inputs <- [worker]Collect Inputs[node] - [conditions]For Backend - { - stream <- [func]Instruction Stream - nfunc <- [func]Do If[~, nstream] - }{ - stream <- Val[func] - nfunc <- Val[nstream] - } - nstream <- Compile Call Node[node, program, stream, inputs, node index] - }{ - If[[[node]Type >>] = ["output"]] - { - inputs <- [worker]Collect Inputs[node] - [conditions]For Backend - { - stream <- [func]Instruction Stream - nfunc <- [func]Do If[~, nstream] - }{ - stream <- Val[func] - nfunc <- Val[nstream] - } - nstream <- [stream]Move[[inputs]Index[0], [[worker]Outputs >>]Index[ [node]Data >> ] ] - }{ - If[[[node]Type >>] = ["const"]] - { - constname <- Const Name[[node]Data >>, node index, [worker]Name >>] - withconst <- [func]Register Constant[constname, [node]Data >>] - [conditions]For Backend - { - stream <- [[withconst]Instruction Stream - ]Move[Strip Addref[Constant[constname]], [[["__result_"]Append[node index]]Append["_"]]Append[0]] - nfunc <- [withconst]Do If[~, stream] - }{ - nfunc <- Val[withconst] - } - }{ - [conditions]For Backend - { - input name <- [[worker]Inputs >>]Index[ [node]Data >> ] - stream <- [[func]Instruction Stream - ]Move[input name, [[["__result_"]Append[node index]]Append["_"]]Append[0]] - nfunc <- [func]Do If[~, stream] - }{ - nfunc <- Val[func] - } - } - - } - } - [nodes]Next[current] - { - out,out worker <- [worker]Compile Node[program,nfunc,nodes,~] - }{ - out <- Val[nfunc] - out worker <- Val[worker] - } -} - -Save Node Conditions@NWorker[worker,node index:out] -{ - node <- [[worker]Nodes >>]Index[node index] - conditions <- [worker]Collect Conditions[node] - out <- [worker]Nodes <<[ [[worker]Nodes >>]Set[node index, [node]Conditions <<[conditions]] ] - -} - -Save Group Conditions@NWorker[worker, groups,current:out] -{ - nodes <- [groups]Index[current] - nworker <- Fold["Save Node Conditions", worker, nodes] - - [groups]Next[current] - { - out <- [nworker]Save Group Conditions[groups,~] - }{ - out <- Val[nworker] - } -} - -Compile Group@NWorker[worker,program,func,groups,current:out,out worker] -{ - nodes <- [groups]Index[current] - [nodes]First - { - nfunc,nworker <- [worker]Compile Node[program,func,nodes,~] - }{ - nfunc <- Val[func] - nworker <- Val[worker] - } - [groups]Next[current] - { - out,out worker <- [nworker]Compile Group[program,nfunc,groups,~] - }{ - out <- Val[nfunc] - out worker <- Val[nworker] - } -} - -Release Var@NWorker[worker,func,name:out] -{ - //__result_index_ionum - parts <- [name]Split["_"] - index <- >]Index[index] - dests <- [[node]Wires From >>]Index[io num] {} - { - Print["oops"] - { Pretty Print[node, ""] - { Pretty Print[parts, ""]}} } - ,normal <- If[[[dests]Length] = [1]] - { - dest ionum <- [[dests]Index[0]]IO Num >> - If[[[[dests]Index[0]]IO Num >>]=[-1]] - { - normal <- Yes - }{ - dest index <- [[dests]Index[0]]Index >> - dest node <- [[worker]Nodes >>]Index[dest index] - - [[dest node]Conditions >>]For Backend - { - out <- [func]Do If[AndCond[NotCond[~], name], [[func]Instruction Stream]Release[name]] - }{ - out <- func - } - } - } - - Val[normal] - { - do if <- If[[[node]Outputs >>] > [1]] {} - { - do if <- [[node]Conditions >>]Empty? {} - { - out <- [func]Release[name] - } - } - - Val[do if] - { - stream <- [[func]Instruction Stream]Release[name] - out <- [func]Do If[name, stream] - } - } -} - -Result Var[vars,io num,index:out] -{ - out <- [vars]Append[[[["__result_"]Append[index]]Append["_"]]Append[io num]] -} - -Node Result Vars[vars,node,index:out] -{ - [("call","getfield","setfield")]Find[[node]Type >>] - { - If[[[node]Type >>]=["call"]] - { - save outs <- [Max Used Output[node, [[node]Outputs >>]-[1]]]+[1] - }{ - save outs <- [node]Outputs >> - } - out <- Fold[["Result Var"]Set Input[2, index], vars, Range[0, save outs]] - }{ - out <- vars - } -} - -Result Vars@NWorker[worker:out] -{ - out <- Fold["Node Result Vars", (), [worker]Nodes >>] -} - -_No Release[vars,node,index,worker:out] -{ - [("const","input")]Find[[node]Type >>] - { - [[node]Conditions >>]For Backend - { - out <- Result Var[vars, 0, index] - }{ - out <- vars - } - }{ - out <- vars - } -} - -No Release Results@NWorker[worker:out] -{ - out <- Fold[["_No Release"]Set Input[3, worker], (), [worker]Nodes >>] -} - -Make Basic Type[type:out] -{ - out <- [Type Instance[[type]Name >>]]Params <<[ [type]Params >> ] -} - -FInputs[ifunc, input type, index, inputs:out] -{ - func <- [ifunc]Set Input Type[Make Basic Type[input type], index] - name <- [inputs]Index[index] - If[[[input type]Variant >>] = ["Naked"]] - { - - naked <- [" naked"]Append[name] - - out <- [[[func]Allocate Var[naked, input type] - ]Unbox[name, naked] - ]Release[name] - }{ - If[[input type]Mutable? >>] - { - name <- [inputs]Index[index] - copied <- [func]Copy[name, name] - - }{ - copied <- Val[func] - } - If[[[input type]Variant >>] = ["Raw Pointer"]] - { - raw <- [" raw"]Append[name] - If[[[input type]Name >>]=["Array"]] - { - - out <- [[copied]Allocate Var[raw, input type] - ]Array Raw Pointer[name, raw] - }{ - out <- [[copied]Allocate Var[raw, input type] - ]Get Raw Pointer[name, raw] - } - }{ - out <- Val[copied] - } - } -} - -Release Raw Inputs[func,input type,index,inputs,outputs:out] -{ - If[[[input type]Variant >>] = ["Raw Pointer"]] - { - name <- [inputs]Index[index] - If[[input type]Mutable? >>] - { - [outputs]Find[[inputs]Index[index]] - { - out <- func - }{ - out <- [func]Release[name] - } - }{ - out <- [func]Release[name] - } - }{ - out <- func - } -} - -FParams[input:out] -{ - iname <- [input]Index[0] - type <- [input]Index[1] - If[[[type]Variant >>] = ["Naked"]] - { out <- [" naked"]Append[iname] } - { - If[[[type]Variant >>] = ["Raw Pointer"]] - { out <- [" raw"]Append[iname] } - { out <- Val[iname] } - } -} -_Return Param[outputs, inputs, input types, index:out,none] -{ - output <- [outputs]Index[index] - [inputs]Find[output] - { - If[[[input types]Index[~]]Mutable? >>] - { - ,none <- [outputs]Next[index] - { - out,none <- _Return Param[outputs, inputs, input types, ~] - } - } { - out <- index - } - }{ - out <- index - } -} - -Return Param[outputs, inputs, input types:out,none] -{ - ,none <- [outputs]First - { out,none <- _Return Param[outputs, inputs, input types, ~] } - -} - -Save Foreign Result[func, output, index, output types, inputs, input types:out] -{ - type <- [output types]Index[index] - If[[[type]Variant >>] = ["Naked"]] - { - out <- [func]Box[[" naked"]Append[output], output, type] - }{ - [inputs]Find[output] - { - If[[[input types]Index[~]]Mutable? >>] - { - out <- [func]Move[output, Output[output]] - }{ - out <- func - } - }{ - out <- func - } - } -} - -Compile Foreign Stub[worker,program,name:out] -{ - ifunc <- [[program]Create Function[name, [worker]Inputs >>, [worker]Outputs >>, "rhope"] - ]Output Types <<[Map[[worker]Output Types >>, "Make Basic Type"]] - - rp num <- Return Param[[worker]Outputs >>, [worker]Inputs >>, [worker]Input Types >>] - { - rbase <- [[worker]Outputs >>]Index[rp num] - If[[[[[worker]Output Types >>]Index[rp num]]Variant >>] = ["Naked"]] - { - rparam <- [" naked"]Append[rbase] - rfunc <- [ifunc]Allocate Var[rparam, [[worker]Output Types >>]Index[rp num]] - }{ - rparam <- Val[rbase] - rfunc <- Val[ifunc] - } - }{ - rparam <- "" - rfunc <- Val[ifunc] - } - - Fold[["FInputs"]Set Input[3, [worker]Inputs >>], rfunc, [worker]Input Types >>] - { [~]Call Foreign[name, [worker]Convention >>, Map[Zip[[worker]Inputs >>, [worker]Input Types >>], "FParams"], rparam] - { Fold[[["Release Raw Inputs"]Set Input[3, [worker]Inputs >>]]Set Input[4, [worker]Outputs >>], ~, [worker]Input Types >>] - { Fold[[[["Save Foreign Result"]Set Input[3, [worker]Output Types >>]]Set Input[4, [worker]Inputs >>]]Set Input[5, [worker]Input Types >>], ~, [worker]Outputs >>] - { out <- [program]Store Function[~] }}}} -} - -Compile Worker@NWorker[worker,program,name:out] -{ - If[[worker]Builtin? >>] - { - out <- program - }{ - If[[[worker]Library >>] = [""]] - { - ifunc <- Fold["Set Output Type", Fold["Set Input Type", [program]Create Function[name,[worker]Inputs >>, [worker]Outputs >>, [worker]Convention >>], [worker]Input Types >>], [worker]Output Types >>] - - - - groups <- [worker]Dependency Groups - [groups]First - { - with conds <- [worker]Save Group Conditions[groups, ~] - final func <- [with conds]Compile Group[program,func,groups, ~] - }{ - final func <- Val[func] - } - res vars <- [worker]Result Vars - init vars <- Concatenate[res vars, [with conds]No Release Results] - - func <- Fold["Set Null", Fold["Set Null", Fold[["Allocate Var"]Set Input[2, "Any Type"], ifunc, init vars], init vars], [worker]Outputs >>] - out <- [program]Store Function[Fold["Release", Fold[["Release Var"]Set Input[0, with conds], final func, res vars], [worker]Inputs >>]] - }{ - out <- Compile Foreign Stub[worker,[program]Link[[worker]Convention >>, [worker]Library >> ],name] - } - } -} - -Test[:out] -{ - ref+ <- Worker Ref["+","rhope",2,1] - ref* <- Worker Ref["*","rhope",2,1] - ,a <- [NWorker["rhope"] - ]Add Input["a", 0] { - ,b <- [~]Add Input["b", 1] { - ,c <- [~]Add Input["c", 2] { - ,outref <- [~]Add Output["out", 0] { - ,call+ <- [~]Add Worker Call[ref+] { - ,call* <- [~]Add Worker Call[ref*] { - out <- [[[[[~]Add Wire[a,0,call+,0] - ]Add Wire[b,0,call+,1] - ]Add Wire[call+,0,call*,0] - ]Add Wire[c,0,call*,1] - ]Add Wire[call*,0,outref,0] - }}}}}} -} - -Test Graph to Backend[:out] -{ - out <- [Test[]]Compile Worker[C Program[], "Test"] -} - -Blueprint NBlueprint -{ - Fields - Methods -} - -NBlueprint[:out] -{ - out <- [[Build["NBlueprint"]]Fields <<[()]]Methods <<[Dictionary[]] -} - -Add Field@NBlueprint[bp,name,type:out] -{ - out <- [bp]Fields <<[ [[bp]Fields >>]Append[ [[()]Append[name]]Append[type] ] ] -} - -Add Method@NBlueprint[bp,name:out] -{ - out <- [bp]Methods <<[ [[bp]Methods >>]Set[name, Yes] ] -} - -Understands Method@NBlueprint[bp,name:out] -{ - out <- [[bp]Methods >>]Index[name] {} - { out <- No } -} - -Get Field Type@NBlueprint[bp,name:out,notfound] -{ - ,notfound <- [[bp]Fields >>]Index[name] - { out <- [~]Index[1] } -} - -_Compile Blueprint Fields[type,field:out] -{ - name <- [field]Index[0] - ftype <- [field]Index[1] - out <- [type]Add Field[name,ftype] -} - -_Compile Blueprint Methods[type,junk,name:out] -{ - If[[[name]=["Call"]] And [[[type]Name >>] = ["Worker"]]] - { - out <- type - }{ - out <- [type]Add Method[name] - } -} - -Make Init[func,field:out] -{ - name <- [field]Index[0] - variant <- [[field]Index[1]]Variant >> - If[[variant] = ["Boxed"]] - { - out <- [func]Set Field Null["obj", name] - }{ - out <- func - } -} - -Make Copy[func,field:out] -{ - name <- [field]Index[0] - variant <- [[field]Index[1]]Variant >> - If[[variant] = ["Boxed"]] - { - got <- [func]Read Field["obj", name] {} - { - stream <- [[got]Instruction Stream - ]AddRef No Dest[~] - out <- [got]Do If[~, stream] - } - }{ - out <- func - } -} - -Make Cleanup[func,field:out] -{ - name <- [field]Index[0] - variant <- [[field]Index[1]]Variant >> - If[[variant] = ["Boxed"]] - { - got <- [func]Read Field["obj", name] {} - { - stream <- [[got]Instruction Stream - ]Release[~] - out <- [got]Do If[~, stream] - } - }{ - out <- func - } -} - -Make Special@NBlueprint[bp,backend,func name,bp name,pop worker:out] -{ - func <- [[backend]Create Function[func name,("obj"),(),"cdecl"] - ]Set Input Type[Type Instance[bp name], 0] - out <- [backend]Store Function[Fold[pop worker, func, [bp]Fields >>]] -} - -Getters Setters[backend,field,type name:out] -{ - //TODO: Throw an exception or something if we read a field that is empty - name <- [field]Index[0] - type <- [field]Index[1] - mytype <- Type Instance[type name] - start getter,getref <- [[[[backend]Create Function[ [[[name]Append[" >>"]]Append["@"]]Append[type name], ("obj"), ("out"), "rhope"] - ]Set Input Type[mytype, 0] - ]Set Output Type[[type]Set Variant["Boxed"], 0] - ]Read Field["obj", name] - If[[[type]Variant >>] = ["Boxed"]] - { - getter <- [[start getter]Do AddRef[getref, "out"]]Release["obj"] - }{ - getter <- [[start getter]Box[getref, "out", type]]Release["obj"] - } - - begin setter <- [[[[[backend]Create Function[ [[[name]Append[" <<"]]Append["@"]]Append[type name], ("obj","newval"), ("out"), "rhope"] - ]Set Input Type[mytype, 0] - ]Set Input Type[[type]Set Variant["Boxed"], 1] - ]Set Output Type[mytype, 0] - ]Copy["obj"] - - If[[[type]Variant >>] = ["Boxed"]] - { - ,origref <- [begin setter]Read Field["obj", name] - { - stream <- [[~]Instruction Stream - ]Release[origref] - ,setref <- [[~]Do If[origref, stream] - ]Write Field["obj", name] - { - setter <- [[~]Move["newval", setref] - ]Move["obj", "out"] - } - } - }{ - ,setref <- [begin setter]Write Field["obj", name] - { - setter <- [[[~]Unbox["newval", setref] - ]Release["newval"] - ]Move["obj", "out"] - } - } - - out <- [[backend]Store Function[getter]]Store Function[setter] - -} - -Compile Blueprint@NBlueprint[bp,backend,name:out] -{ - //Rhope identifiers can't start with spaces, so we can use identifiers that start with spaces for special functions - init name <- [" init "]Append[name] - [("Array","Boxed Array","Worker")]Find[name] - { - [("internalarraynaked","internalarrayboxed","internalworker")]Index[~] - { - copy name <- [~]Append["copy"] - cleanup name <- [~]Append["cleanup"] - } - }{ - copy name <- [" copy "]Append[name] - cleanup name <- [" cleanup "]Append[name] - } - type <- [[[Fold["_Compile Blueprint Methods", Fold["_Compile Blueprint Fields", [backend]Create Type[name], [bp]Fields >>], [bp]Methods >>] - ]Init <<[init name] - ]Copy <<[copy name] - ]Cleanup <<[cleanup name] - - out <- [backend]Register Type[type] -} - -Compile Special@NBlueprint[bp,backend,name:out] -{ - init name <- [" init "]Append[name] - [("Array","Boxed Array","Worker")]Find[name] - { - after copyclean <- Val[backend] - }{ - copy name <- [" copy "]Append[name] - cleanup name <- [" cleanup "]Append[name] - after copyclean <- [bp]Make Special[ - [bp]Make Special[backend, copy name, name, "Make Copy"], - cleanup name, name, "Make Cleanup"] - } - out <- Fold[["Getters Setters"]Set Input[2, name], [bp]Make Special[after copyclean, init name, name, "Make Init"], [bp]Fields >>] -} - -Blueprint NProgram -{ - Blueprints - Workers - Worker Refs -} - -NProgram[:out] -{ - out <- [[[Build["NProgram"]]Blueprints <<[Dictionary[]]]Workers <<[Dictionary[]]]Worker Refs <<[Dictionary[]] -} - -Bind Worker@NProgram[prog,name,worker:out] -{ - after bind <- [prog]Workers << [ [[prog]Workers >>]Set[name, [worker]Name <<[name]] ] - parts <- [name]Split["@"] - [parts]Index[1] - { - orig bp <- [[after bind]Blueprints >>]Index[~] {} - { orig bp <- NBlueprint[] } - out <- [after bind]Blueprints <<[ [[after bind]Blueprints >>]Set[~, [orig bp]Add Method[[parts]Index[0]] ] ] - }{ - out <- Val[after bind] - } -} - -Bind Blueprint@NProgram[prog,name,blueprint:out] -{ - out <- [prog]Blueprints << [ [[prog]Blueprints >>]Set[name, blueprint] ] -} - -_Compile Program BP[backend, blueprint, name:out] -{ - out <- [blueprint]Compile Blueprint[backend, name] -} - -_Compile Program BP Special[backend, blueprint, name:out] -{ - out <- [blueprint]Compile Special[backend, name] -} - -_Compile Program[backend, worker, name:out] -{ - out <- [worker]Compile Worker[backend, name] -} - -Compile Program@NProgram[prog, backend:out] -{ - backend with bps <- Generate Boolean Methods[Generate Number Methods[Fold["_Compile Program BP Special", Fold["_Compile Program BP", backend, [prog]Blueprints >>], [prog]Blueprints >>]]] - workers with infer <- SMap[[prog]Workers >>, ["Infer Types"]Set Input[1, prog]] - out <- Fold["_Compile Program", backend with bps, workers with infer] -} - -Register Method@NProgram[prog, name, convention, inputs, outputs: out] -{ - [[prog]Worker Refs >>]Index[name] - { - ref <- [[[[~]Inputs <<[ Max[[~]Inputs >>, inputs] ] - ]Min Inputs <<[ Min[[~]Min Inputs >>, inputs] ] - ]Outputs <<[ Max[[~]Outputs >>, outputs] ] - ]Min Outputs <<[ Min[[~]Min Outputs >>, outputs] ] - }{ - ref <- Worker Ref[name, convention, inputs, outputs, Yes] - } - out <- [prog]Worker Refs <<[ [[prog]Worker Refs >>]Set[name, ref]] -} - -Register Worker@NProgram[prog, name, convention, inputs, outputs: out] -{ - [[prog]Worker Refs >>]Index[name] - { - ref <- [[[[~]Inputs <<[ Max[[~]Inputs >>, inputs] ] - ]Min Inputs <<[ Min[[~]Min Inputs >>, inputs] ] - ]Outputs <<[ Max[[~]Outputs >>, outputs] ] - ]Min Outputs <<[ Min[[~]Min Outputs >>, outputs] ] - }{ - ref <- Worker Ref[name, convention, inputs, outputs, No] - } - after reg <- [prog]Worker Refs <<[ - [ [prog]Worker Refs >> ]Set[name, ref] - ] - - parts <- [name]Split["@"] - [parts]Index[1] - { - out <- [after reg]Register Method@NProgram[[parts]Index[0], convention, inputs, outputs] - }{ - out <- Val[after reg] - } -} - -Register Builtins@NProgram[prog:out] -{ - registered <- [[[[[[[[prog]Register Worker["Print", "rhope", 1, 1] - ]Register Worker["If@Boolean", "rhope", 1, 2] - ]Register Worker["Build", "rhope", 1, 1] - ]Register Worker["Blueprint Of", "rhope", 1, 1] - ]Register Worker["Call@Worker", "rhope", 1, 2] //We're using 2 because we need to assume that the outputs are conditional - ]Register Worker["ID", "rhope", 1, 1] - ]Register Worker["Blueprint From ID", "rhope", 1, 2] - ]Register Number Methods - - out <- [[[[[[[registered]Bind Worker["If@Boolean", - [[[[[NWorker["rhope"] - ]Inputs <<[("condition")] - ]Input Types <<[ [()]Append[Type Instance["Boolean"]] ] - ]Outputs <<[("isyes","isno")] - ]Output Types <<[ [[()]Append[Type Instance["Boolean"]]]Append[Type Instance["Boolean"]] ] - ]Builtin? <<[Yes]] - ]Bind Worker["Print", - [[[[[NWorker["rhope"] - ]Inputs <<[("value")] - ]Input Types <<[ [()]Append[Type Instance["Any Type"]] ] - ]Outputs <<[("out")] - ]Output Types <<[ [()]Append[Type Instance["Int32"]] ] - ]Builtin? <<[Yes]] - ]Bind Worker["Build", - [[[[[NWorker["rhope"] - ]Inputs <<[("type")] - ]Input Types <<[ [()]Append[Type Instance["Blueprint"]] ] - ]Outputs <<[("out")] - ]Output Types <<[ [()]Append[Type Instance["Any Type"]] ] - ]Builtin? <<[Yes]] - ]Bind Worker["Blueprint Of", - [[[[[NWorker["rhope"] - ]Inputs <<[("object")] - ]Input Types <<[ [()]Append[Type Instance["Any Type"]]] - ]Outputs <<[("type")] - ]Output Types <<[ [()]Append[Type Instance["Blueprint"]]] - ]Builtin? <<[Yes]] - ]Bind Worker["Call@Worker", - [[[[[NWorker["rhope"] - ]Inputs <<[("worker")] - ]Input Types <<[ [()]Append[Type Instance["Worker"]] ] - ]Outputs <<[("ret1","ret2")] - ]Output Types <<[ [[()]Append[Type Instance["Any Type"]]]Append[Type Instance["Any Type"]] ] - ]Builtin? << [Yes]] - ]Bind Worker["ID", - [[[[[NWorker["rhope"] - ]Inputs <<[("bp")] - ]Input Types <<[ [()]Append[Type Instance["Blueprint"]]] - ]Outputs <<[("id")] - ]Output Types <<[ [()]Append[Type Instance["UInt32"]]] - ]Builtin? << [Yes]] - ]Bind Worker["Blueprint From ID", - [[[[[NWorker["rhope"] - ]Inputs <<[("id")] - ]Input Types <<[ [()]Append[Type Instance["UInt32"]]] - ]Outputs <<[("bp","none")] - ]Output Types <<[ [[()]Append[Type Instance["Blueprint"]]]Append[Type Instance["Any Type"]]] - ]Builtin? << [Yes]] -} - -Find Worker@NProgram[prog, name:out,notfound] -{ - out,notfound <- [[prog]Worker Refs >>]Index[name] -} - -Find Worker Def@NProgram[prog,name:out,notfound] -{ - out,notfound <- [[prog]Workers >>]Index[name] -} - -Find Method@NProgram[prog, name, type:out,notfound] -{ - bp,notfound <- [[prog]Blueprints >>]Index[[type]Name >>] - ,notfound <- If[[bp]Understands Method[name]] - { - out <- [[prog]Workers >>]Index[[[name]Append["@"]]Append[[type]Name >>]] - } -} - -Find Field@NProgram[prog, name, type:fieldtype,notfound] -{ - bp,notfound <- [[prog]Blueprints >>]Index[[type]Name >>] - fieldtype,notfound <- [bp]Get Field Type[name] -} - -Implicit Conversion@NProgram[prog, fromtype, totype:func,notfound] -{ - notfound <- No -} - -Is Method?@NProgram[prog,name:is,is not] -{ - ,is not <- [[prog]Worker Refs>>]Index[name] - { - is,is not <- If[[~]Is Method? >>] - } -} diff -r db30bda91aad -r 24c6f8767190 parser_old.rhope --- a/parser_old.rhope Wed Jul 27 21:06:57 2011 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1653 +0,0 @@ -Import nworker.rhope - -Blueprint Parser -{ - Arg Begin - Arg End - Line Comment - Comment Begin - Comment End - Assign - Block Begin - Block End - Blueprint Type Delim - Empty Block - Binary Operator - String Begin - String End - String Escape - List Begin - List End - List Delim - In Out Delim - Do Worker - Index Begin - Index End - Previous - Block Val - Set Field - Get Field - Import - Blueprint - Global Separator - Uses - Hex Escape - Escape Map - Foreign -} - -New@Parser[:out] -{ - out <- [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[Build["Parser"] - ]Arg Begin << ["["] - ]Arg End <<["]"] - ]Line Comment <<["//"] - ]Comment Begin <<["/*"] - ]Comment End <<["*/"] - ]Assign <<["<-"] - ]Block Begin <<["{"] - ]Block End <<["}"] - ]Blueprint Type Delim <<[":"] - ]Empty Block <<[";"] - ]Binary Operator <<["`"] - ]String Begin <<["\""] - ]String End <<["\""] - ]String Escape <<["\\"] - ]List Begin <<["("] - ]List End <<[")"] - ]List Delim <<[","] - ]In Out Delim <<[":"] - ]Do Worker <<["$"] - ]Index Begin <<["("] - ]Index End <<[")"] - ]Previous <<["@"] - ]Set Field <<["<<"] - ]Get Field <<[">>"] - ]Import <<["Import"] - ]Blueprint <<["Blueprint"] - ]Global Separator <<["::"] - ]Hex Escape <<["x"] - ]Uses <<["uses"] - ]Escape Map <<[[[[Dictionary[]]Set["n","\n"]]Set["r","\r"]]Set["t","\t"]] - ]Foreign <<["Foreign"] -} - -Blueprint Output Reference -{ - Index - Output Number -} - -New@Output Reference[index,num:out] -{ - out <- [[Build["Output Reference"]]Index <<[index]]Output Number <<[num] -} - -Add Pipe Reference[refs,name,reference:out] -{ - reflist <- [refs]Index[name] {} - { - reflist <- () - } - out <- [refs]Set[name, [reflist]Append[reference]] -} - -Assignment Save Reference[refs,assignment,output num,parse worker,index:out] -{ - [[parse worker]Outputs >>]Find[assignment] - { - out <- refs - }{ - out <- Add Pipe Reference[refs, assignment, New@Output Reference[index, output num]] - } -} - -Blueprint Foreign Lib -{ - Language - Name -} - -New Foreign Lib[language, library:out] -{ - out <- [[Build["Foreign Lib"] - ]Language <<[language] - ]Name <<[library] -} - -Blueprint Parse Program -{ - Workers - Imports - Blueprints - Errors -} - -New@Parse Program[:out] -{ - out <- [[[Build["Parse Program"] - ]Workers <<[Dictionary[]] - ]Imports <<[[Dictionary[]]Set["kernel.rhope", Yes]] - ]Blueprints <<[Dictionary[]] -} - -Blueprint Blueprint Definition -{ - Name - Fields -} - -New@Blueprint Definition[name,fields:out] -{ - out <- [[Build["Blueprint Definition"]]Name << [name]]Fields <<[fields] -} - -Blueprint Parse Worker -{ - Name - Inputs - Outputs - Line Number - Trees - Uses Stores - Input Types - Output Types -} - -New@Parse Worker[name,inputs,outputs,intypes,outtypes,line:out] -{ - out <- [[[[[[[[Build["Parse Worker"]]Name <<[name]]Inputs <<[inputs]]Outputs <<[outputs]]Line Number <<[line]]Trees <<[()]]Uses Stores <<[()]]Input Types <<[intypes]]Output Types <<[outtypes] -} - -Blueprint Worker Node -{ - Name - Params - Assignments - Blocks - Index -} - -New@Worker Node[name,params:out] -{ - out <- [[[[Build["Worker Node"]]Name <<[name]]Params <<[params]]Assignments <<[()]]Blocks <<[()] -} - -Add List Helper[inlist,worker,program,key,parse worker,refs:out list,out worker,out refs] -{ - ,nextworker,nextrefs <- [[inlist]Index[key]]Add to Worker[worker, program, parse worker, refs] - { nextlist <- [inlist]Set[key, ~] } - [inlist]Next[key] - { - out list,out worker,out refs <- Add List Helper[nextlist, nextworker, program, ~, parse worker, nextrefs] - }{ - out list <- Val[nextlist] - out worker <- Val[nextworker] - out refs <- Val[nextrefs] - } -} - -Add List to Worker[list,worker,program,parse worker,refs:out list,out worker,out refs] -{ - [list]First - { - out list,out worker,out refs <- Add List Helper[list, worker, program, ~, parse worker, refs] - }{ - out list <- list - out worker <- worker - out refs <- refs - } -} - -_Add Blocks to Worker[blocks,worker,program,parse worker,key,refs:out blocks,out worker,out refs] -{ - block, next worker, nextrefs <- Add List to Worker[[blocks]Index[key], worker, program, parse worker, refs] - next blocks <- [blocks]Set[key, block] - [blocks]Next[key] - { - out blocks,out worker,out refs <- _Add Blocks to Worker[next blocks, next worker, program, parse worker, ~, nextrefs] - }{ - out blocks <- Val[next blocks] - out worker <- Val[next worker] - out refs <- Val[nextrefs] - } -} - -Add Blocks to Worker[blocks,worker,program,parse worker,refs:out blocks,out worker,out refs] -{ - [blocks]First - { - out blocks, out worker, out refs <- _Add Blocks to Worker[blocks, worker, program, parse worker, ~, refs] - }{ - out blocks <- blocks - out worker <- worker - out refs <- refs - } -} - -Add to Worker@Worker Node[node,worker,program,parse worker,refs:out node,out worker,out refs] -{ - [program]Find Worker[[node]Name >>] - { - after worker <- [worker]Add Worker Call[~] {} - { - //Print[[[[node]Name >>]Append[" has index "]]Append[~]] - assignment refs <- Fold[[["Assignment Save Reference"]Set Input[3, parse worker]]Set Input[4, ~], refs, [node]Assignments >>] - [node]Index <<[~] - { - params list, params worker, params refs <- Add List to Worker[[~]Params >>, after worker, program, parse worker, assignment refs] - block list, out worker, out refs <- Add Blocks to Worker[[~]Blocks >>, params worker, program, parse worker, params refs] - out node <- [[~]Params <<[params list]]Blocks <<[block list] - } - } - }{ - Print[["Error: Could not find a worker named "]Append[[node]Name >>]] - } -} - -Check Question Mark[params, index:has mark,no mark] -{ - param,no mark <-[params]Index[index] - { - If[[Type Of[param]]=["Named Pipe Node"]] - { - has mark <-If[[[param]Name >>] = ["?"]] {} - { - has mark,no mark <- Check Question Mark[params, [index]+[1]] - } - }{ - has mark,no mark <- Check Question Mark[params, [index]+[1]] - } - } -} - -Collect Literal Inputs[literal, params, index, complex inputs:literal out,complex out] -{ - param <- [params]Index[index] - { - If[[Type Of[param]]=["Literal Node"]] - { - next literal <- [literal]Set Input[index, [param]Value >>] - next complex <- Val[complex inputs] - }{ - ,doset <- If[[Type Of[param]]=["Named Pipe Node"]] - { - ,doset <- If[[[param]Name >>] = ["?"]] - { - //Question mark indicates unpopulated input - next complex <- Val[complex inputs] - next literal <- Val[literal] - } - } - - Val[doset] - { - next complex <- [complex inputs]Set[index, param] - next literal <- Val[literal] - } - } - literal out, complex out <- Collect Literal Inputs[next literal, params, [index]+[1], next complex] - }{ - literal out <- literal - complex out <- complex inputs - } -} - -Do Set Input[literal,param,index:out] -{ - out <- New@Worker Node["Set Input", [[[()]Append[literal]]Append[New@Literal Node[index]]]Append[param]] -} - -Check Worker Literals@Worker Node[node,program:out] -{ - new params <- Map[[node]Params >>, ["Check Worker Literals"]Set Input[1, program]] - Check Question Mark[new params, 0] - { - base literal, complex inputs <- Collect Literal Inputs[Worker Literal[[node]Name >>], new params, 0, ()] - new node <- Fold["Do Set Input", New@Literal Node[base literal], complex inputs] - }{ - new node <- [node]Params <<[new params] - } - out <- [new node]Blocks <<[ Map[[node]Blocks >>, ["Map"]Set Input[1, ["Check Worker Literals"]Set Input[1, program]]] ] -} - -Add Multi Wire[worker,ref,junk,end index,input num:out] -{ - out <- [worker]Add Wire[[ref]Index >>, [ref]Output Number >>, end index, input num] -} - -Add Param Wire[worker,param,input num,end index,blocks,parse worker,assignments:out] -{ - param worker, start index, output num <- [param]Add Wires[worker, blocks, parse worker, assignments] {} - { - out <- [param worker]Add Wire[start index, output num, end index, input num] - }{}{ - out <- Fold[[["Add Multi Wire"]Set Input[3, end index]]Set Input[4, input num], param worker, ~] - } -} - -_Add Block Wire[worker,node,junk,blocks,parse worker,assignments:out] -{ - out <- [node]Add Wires[worker, blocks, parse worker, assignments] -} - -Add Block Wire[worker,block nodes,output num,parent index,existing blocks,parse worker,assignments:out] -{ - blocks <- [existing blocks]Append[New@Output Reference[parent index, output num]] - out <- Fold[[[["_Add Block Wire"]Set Input[3, blocks]]Set Input[4, parse worker]]Set Input[5, assignments], worker, block nodes] -} - -Assignments Add Wires[worker,assignment,output num,parse worker,start index:out worker] -{ - [[parse worker]Outputs >>]Find[assignment] - { - ,output index <- [worker]Add Typed Output[assignment, ~, [[parse worker]Output Types >>]Index[~]] - { - out worker <- [~]Add Wire[start index, output num, output index, 0] - } - }{ - //Ugly hack alert! - If[[assignment]Contains["::"]] - { - parts <- [assignment]Split["::"] - ,global index <- [worker]Add Global Set[[parts]Index[0], [parts]Index[1]] - { - out worker <- [~]Add Wire[start index, output num, global index, 0] - } - }{ - out worker <- worker - } - } -} - -Has Block@Worker Node[junk:out,unused] -{ - out <- Yes -} - -_Has Block Params[param list,key:out] -{ - param <- [param list]Index[key] - out <- [param]Has Block {} - { - [param list]Next[key] - { - out <- _Has Block Params[param list, ~] - }{ - out <- No - } - } - -} - -Has Block Params[param list:out] -{ - [param list]First - { - out <- _Has Block Params[param list, ~] - }{ - out <- No - } -} - -Add Wires@Worker Node[node,worker,blocks,parse worker,assignments:worker,index,num,unused] -{ - worker,index,num <- Add Wires Worker or Field[node, worker, blocks, parse worker, assignments] -} - -Add Wires Worker or Field[node,worker,blocks,parse worker,assignments:worker,index,num,unused] -{ - Fold[[["Assignments Add Wires"]Set Input[3, parse worker]]Set Input[4, [node]Index >>], worker, [node]Assignments >>] - { - Fold[[[[["Add Block Wire"]Set Input[3, [node]Index >>]]Set Input[4, blocks]]Set Input[5, parse worker]]Set Input[6, assignments], ~, [node]Blocks >>] - { - params worker <- Fold[[[[["Add Param Wire"]Set Input[3, [node]Index >>]]Set Input[4, blocks]]Set Input[5, parse worker]]Set Input[6, assignments], ~, [node]Params >>] - { - index <- [node]Index >> - num <- 0 - } - } - } - If[Has Block Params[[node]Params >>]] - { - worker <- Val[params worker] - }{ - [blocks]Peek - { - worker <- [params worker]Add Wire[[~]Index >>, [~]Output Number >>, [node]Index >>, [0]-[1]] - }{ - worker <- Val[params worker] - } - } -} - -Blueprint Field Node -{ - Name - Params - Assignments - Blocks - Index - Set? -} - -Has Block@Field Node[junk:has block,unused] -{ - has block <- Yes -} - -New@Field Node[name,params,set:out] -{ - out <- [[[[[Build["Field Node"]]Name <<[name]]Assignments <<[()]]Blocks <<[()]]Set? <<[set]]Params <<[params] -} - -Add to Worker@Field Node[node,worker,program,parse worker,refs:out node,out worker,out refs] -{ - If[[node]Set? >>] - { - after worker,index <- [worker]Add Object Set[[node]Name >>] - }{ - after worker,index <- [worker]Add Object Get[[node]Name >>] - } - Val[index] - { - assignment refs <- Fold[[["Assignment Save Reference"]Set Input[3, parse worker]]Set Input[4, ~], refs, [node]Assignments >>] - [node]Index <<[~] - { - params list, params worker, params refs <- Add List to Worker[[~]Params >>, after worker, program, parse worker, assignment refs] - block list, out worker, out refs <- Add Blocks to Worker[[~]Blocks >>, params worker, program, parse worker, params refs] - out node <- [[~]Params <<[params list]]Blocks <<[block list] - } - } -} - -Add Wires@Field Node[node,worker,blocks,parse worker,assignments:worker,index,num] -{ - worker,index,num <- Add Wires Worker or Field[node, worker, blocks, parse worker, assignments] -} - -Check Worker Literals@Field Node[node,program:out] -{ - new params <- Map[[node]Params >>, ["Check Worker Literals"]Set Input[1, program]] - out <- [[node]Blocks <<[ Map[[node]Blocks >>, ["Map"]Set Input[1, ["Check Worker Literals"]Set Input[1, program]]] ] - ]Params <<[new params] -} - -Blueprint Named Pipe Node -{ - Name - Assignments - Blocks - Index -} - -Has Block@Named Pipe Node[node:has block,no block] -{ - If[[[node]Index >>] < [0]] - { - //~ should really be a parser parameter - If[[[node]Name >>] = ["~"]] - { - has block <- Yes - }{ - no block <- No - } - }{ - has block <- Yes - } -} - -New@Named Pipe Node[name:out] -{ - out <- [[[Build["Named Pipe Node"]]Name <<[name]]Assignments <<[()]]Blocks <<[()] -} - -Add to Worker@Named Pipe Node[node,worker,program,parse worker,refs:out node,out worker,out refs] -{ - [[parse worker]Inputs >>]Find[[node]Name >>] - { - after add <- [worker]Add Typed Input[[node]Name >>, ~, [[parse worker]Input Types>>]Index[~]] {} - { - assign refs <- Fold[[["Assignment Save Reference"]Set Input[3, parse worker]]Set Input[4, ~], refs, [node]Assignments >>] - index node <- [node]Index <<[~] - } - }{ - after add <- worker - index node <- [node]Index <<[[0]-[1]] - //TODO: Handle assignments from a named pipe that isn't an input - assign refs <- refs - } - block list, out worker, out refs <- Add Blocks to Worker[[node]Blocks >>, after add, program, parse worker, assign refs] - out node <- [index node]Blocks <<[block list] -} - -Add Wires@Named Pipe Node[node,worker,blocks,parse worker,assignments:worker,index,num,reflist] -{ - reflist <- [assignments]Index[[node]Name >>] - { - //TODO: Fix support for a named pipe with a block - worker <- worker - }{ - If[[[node]Name >>] = ["~"]] - { - wires worker <- worker - [blocks]Peek - { - my index <- [~]Index >> - num <- [~]Output Number >> - }{ - //TODO: Propagate an error rather than printing it out - Print["Error, block reference symbol located outside of a block"] - } - }{ - If[[[node]Index >>] < [0]] - { - Print[[[["Error, reference to named pipe "]Append[[node]Name >>]]Append[" that was never assigned to in worker "]]Append[[parse worker]Name >>]] - }{ - my index <- [node]Index >> - num <- 0 - assignments worker <- Fold[[["Assignments Add Wires"]Set Input[3, parse worker]]Set Input[4, [node]Index >>], worker, [node]Assignments >>] - [blocks]Peek - { - wires worker <- [assignments worker]Add Wire[[~]Index >>, [~]Output Number >>, [node]Index >>, [0]-[1]] - }{ - wires worker <- Val[assignments worker] - } - } - } - } - index <- Val[my index] - - worker <- Fold[[[[["Add Block Wire"]Set Input[3, my index]]Set Input[4, blocks]]Set Input[5, parse worker]]Set Input[6, assignments], wires worker, [node]Blocks >>] -} - -Check Worker Literals@Named Pipe Node[node,program:out] -{ - out <- [node]Blocks <<[ Map[[node]Blocks >>, ["Map"]Set Input[1, ["Check Worker Literals"]Set Input[1, program]]] ] -} - -Blueprint Global Node -{ - Store - Name - Assignments - Blocks - Index -} - -New@Global Node[store,name:out] -{ - out <- [[[[Build["Global Node"]]Store <<[store]]Name <<[name]]Assignments <<[()]]Blocks <<[()] -} - -Add to Worker@Global Node[node,worker,unused,parse worker,refs:out node,out worker,refs] -{ - out worker <- [worker]Add Global Get[[node]Store >>, [node]Name >>] {} - { - refs <- Fold[[["Assignment Save Reference"]Set Input[3, parse worker]]Set Input[4, ~], refs, [node]Assignments >>] - out node <- [node]Index <<[~] - } - refs <- refs -} - -Add Wires@Global Node[node,worker,blocks,parse worker,assignments:worker,index,num,unused] -{ - worker,index,num <- Add Wires Literal or Global[node, worker, blocks, parse worker, assignments] -} - -Has Block@Global Node[junk:out,unused] -{ - out <- Yes -} - -Check Worker Literals@Global Node[node,program:out] -{ - out <- [node]Blocks <<[ Map[[node]Blocks >>, ["Map"]Set Input[1, ["Check Worker Literals"]Set Input[1, program]]] ] -} - -Blueprint Literal Node -{ - Value - Assignments - Blocks - Index -} - -Has Block@Literal Node[junk:out,unused] -{ - out <- Yes -} - -New@Literal Node[value:out] -{ - out <- [[[Build["Literal Node"]]Value <<[value]]Assignments <<[()]]Blocks <<[()] -} - -Add to Worker@Literal Node[node,worker,unused,parse worker,refs:out node,out worker,out refs] -{ - out worker <- [worker]Add Constant[[node]Value >>] {} - { - out refs <- Fold[[["Assignment Save Reference"]Set Input[3, parse worker]]Set Input[4, ~], refs, [node]Assignments >>] - out node <- [node]Index <<[~] - } -} - -Add Wires@Literal Node[node,worker,blocks,parse worker,assignments:worker,index,num,unused] -{ - worker,index,num <- Add Wires Literal or Global[node, worker, blocks, parse worker, assignments] -} - -Check Worker Literals@Literal Node[node,program:out] -{ - out <- [node]Blocks <<[ Map[[node]Blocks >>, ["Map"]Set Input[1, ["Check Worker Literals"]Set Input[1, program]]] ] -} - -Add Wires Literal or Global[node,worker,blocks,parse worker,junk:worker,index,num,unused] -{ - assignments worker <- Fold[[["Assignments Add Wires"]Set Input[3, parse worker]]Set Input[4, [node]Index >>], worker, [node]Assignments >>] - [blocks]Peek - { - worker <- [assignments worker]Add Wire[[~]Index >>, [~]Output Number>>, [node]Index >>, [0]-[1]] - }{ - worker <- Val[assignments worker] - } - index <- [node]Index >> - num <- 0 -} - -Blueprint Block Node -{ - Number -} - -Blueprint Parse Error -{ - Type - Text - Line Number -} - -New@Parse Error[type,text,number:out] -{ - out <- [[[Build["Parse Error"]]Type <<[type]]Text <<[text]]Line Number <<[number] -} - -Not Empty[string:out] -{ - If[[[string]Length] > [0]] - { - out <- Yes - }{ - out <- No - } -} - -Blueprint Blueprint Field -{ - Name - Type -} - -New@Blueprint Field[name,type:out] -{ - out <- [[Build["Blueprint Field"]]Name <<[name]]Type <<[type] -} - -Process Blueprint Field[list,field,params:out] -{ - ,name <- [field]Get DString[[params]List Begin >>] - { - type info <- Parse List[~,params,()] - type <- [type info]Index[0] - { - If[[Type Of[~]] = ["Named Pipe Node"]] - { - before variant <- Type Instance[[type]Name >>] - }{ - before variant <- [type]Params <<[ Map[[type]Params >>, "Remove Named Pipe Node"] ] - } - variant <- [type info]Index[1] - { - ,warn <- If[[Type Of[~]] = ["Named Pipe Node"]] - { - fieldtype,warn <- [before variant]Set Variant[[variant]Name >>] - } - Val[warn] - { - Print[[[["Warning: Invalid variant for type "]Append[[before variant]Name >>]]Append[" on field "]]Append[name]] - fieldtype <- Val[before variant] - } - }{ - fieldtype <- Val[before variant] - } - }{ - fieldtype <- Type Instance["Any Type"] - } - out <- [list]Append[New@Blueprint Field[name, fieldtype]] - } {} {} { - out <- [list]Append[New@Blueprint Field[name, Type Instance["Any Type"]]] - } -} - -Block Comment[string,begin comment,end comment,block count:out] -{ - If[[block count] > [0]] - { - after, before <- [string]Get DString[[[()]Append[begin comment]]Append[end comment]] {} {} - { - If[[~] = [begin comment]] - { - out <- Block Comment[after, begin comment, end comment, [block count]+[1]] - }{ - out <- Block Comment[after, begin comment, end comment, [block count]-[1]] - } - }{ - //No match - out <- "" - } - }{ - out <- string - } -} - -Line Comment[string:out] -{ - [string]Get DString["\n"] - { - out <- ["\n"]Append[~] - } {} {} { - out <- "" - } -} - -_Get Comment DString[string,delims,line comment,begin comment,end comment,prev before:rest,before,delim,nomatch] -{ - after,befored,used delim,nomatch <- [string]Get DString[delims] - { - If[[used delim] = [line comment]] - { - after comment <- Line Comment[after] - }{ - If[[used delim] = [begin comment]] - { - after comment <- Block Comment[after, begin comment, end comment, 1] - }{ - rest <- Val[after] - before <- [prev before]Append[befored] - delim <- Val[used delim] - } - } - } {} {} { - before <- [prev before]Append[befored] - } - - after comment - { - rest,more before,delim,nomatch <- _Get Comment DString[~, delims, line comment, begin comment, end comment, prev before] - before <- [before]Append[more before] - } - -} - -Get Comment DString[string,delims,params:rest,before,delim,not found] -{ - line comment <- [params]Line Comment >> - begin comment <- [params]Comment Begin >> - end comment <- [params]Comment End >> - all delims <- [[[delims]As List]Append[begin comment]]Append[line comment] - rest, before, delim, not found <- _Get Comment DString[string, delims, line comment, begin comment, end comment, ""] -} - -Comment Left Trim[string,trim chars,params:out] -{ - line comment <- [params]Line Comment >> - - end comment <- [params]Comment End >> - trimmed <- Left Trim[string, trim chars] - If[[trimmed]Starts With[line comment]] - { - ,after delim <- [trimmed]Slice[[line comment]Length] - out <- Comment Left Trim[Line Comment[after delim], trim chars, params] - }{ - begin comment <- [params]Comment Begin >> - If[[trimmed]Starts With[begin comment]] - { - ,after delim <- [trimmed]Slice[[line comment]Length] - out <- Comment Left Trim[Block Comment[after delim, begin comment, end comment, 1], trim chars, params] - }{ - out <- Val[trimmed] - } - } -} - -PBlueprint[string,params,tree,lines:out] -{ - ,whitespace name <- [string]Get Comment DString[[params]Block Begin >>, params] - { - ,no blueprint <- [whitespace name]Slice[ [[params]Blueprint >>]Length ] - name <- Trim[no blueprint, "\r\n\t "] - name lines <- 0 - ,body <- [~]Get Comment DString[ [params]Block End >>, params] - { - body lines <- [body]Split["\n"] - more lines <- [[[body lines]Length] - [1]] + [name lines] - fields <- Fold[["Process Blueprint Field"]Set Input[2, params], (), Filter[Map[body lines, ["Trim"]Set Input[1,"\n\r\t "]], "Not Empty"]] - new tree <- [tree]Blueprints << [ [[tree]Blueprints >>]Set[name, New@Blueprint Definition[name, fields]] ] - out <- Null[~, params, new tree, [lines] + [more lines]] - } {} { - out <- [tree]Errors <<[ [[tree]Errors >>]Append[New@Parse Error["Error",[["Blueprint is missing an block close symbol \""]Append[[params]Block End >>]]Append["\""], lines]] ] - } - - } {} { - out <- [tree]Errors <<[ [[tree]Errors >>]Append[New@Parse Error["Error",[["Blueprint is missing an block open symbol \""]Append[[params]Block Begin >>]]Append["\""], lines]] ] - } -} - -Parse Import[string,params,tree,lines:out] -{ - [line]Slice[ [[params]Import >>]Length ] {} - { - filename <- Trim[~, " \n\r\t"] - } - new tree <- [tree]Imports <<[ [[tree]Imports >>]Set[filename, Yes] ] - ,line <- [string]Get Comment DString["\n", params] - { - out <- Null[~, params, new tree, [lines] + [1]] - } {} { - out <- Val[new tree] - } -} - -Get Expression Blocks[string,params,blocks:blocks,after] -{ - check block <- Comment Left Trim[string, "\n\r\t ", params] - If[[check block]Starts With[[params]Block Begin >>]] - { - ,begin block <- [check block]Slice[[[params]Block Begin >>]Length] - trees, after block <- Worker Body[begin block, params, ()] - blocks, after <- Get Expression Blocks[after block, params, [blocks]Append[trees]] - }{ - If[[check block]Starts With[[params]Empty Block >>]] - { - blocks <- blocks - ,after <- [check block]Slice[[[params]Empty Block >>]Length] - }{ - blocks <- blocks - after <- Val[check block] - } - } -} - -Parse Escape[string,params:char,after] -{ - code,rest <- [string]Slice[1] - If[[code] = [[params]Hex Escape >>]] - { - hex,after <- [rest]Slice[2] - char <- [""]Put Byte[From Hex@Whole Number[hex]] - }{ - after <- Val[rest] - char <- [[params]Escape Map >>]Index[code] {} - { - char <- Val[code] - } - } -} - -Parse String[string,params,current:value,after] -{ - delims <- [[()]Append[[params]String End >>]]Append[[params]String Escape >>] - afters, before, delim <- [string]Get Comment DString[delims, params] - { - If[[delim] = [[params]String End >>]] - { - value <- [current]Append[before] - after <- Val[afters] - }{ - char,after escape <- Parse Escape[afters, params] - value,after <- Parse String[after escape, params, [[current]Append[before]]Append[char]] - } - } -} - -Parse List[string,params,list:value,after] -{ - trimmed <- Comment Left Trim[string, "\r\n\t ", params] - If[[trimmed]Starts With[[params]List End >>]] - { - value <- list - ,after <- [trimmed]Slice[[[params]List End >>]Length] - }{ - If[[trimmed]Starts With[[params]List Delim >>]] - { - ,el string <- [trimmed]Slice[[[params]List Delim >>]Length] - }{ - el string <- Val[trimmed] - } - element,after el <- Named Pipe or Literal[el string, params] - value,after <- Parse List[after el, params, [list]Append[[element]Get Value]] - } -} - -Get Value@Literal Node[node:out] -{ - out <- [node]Value >> -} - -Get Value@Named Pipe Node[node:out] -{ - out <- node -} - -Blueprint Machine Integer -{ - Value - Size - Signed? -} - -Machine Integer[value,size,signed?:out] -{ - out <- [[[Build["Machine Integer"]]Value <<[value]]Size <<[size]]Signed? <<[signed?] -} - -Parse Number[string,params:value,after] -{ - delims <- [[[[(" ","\t","\n","\r")]Append[[params]List Delim >>]]Append[[params]Block Begin >>]]Append[[params]Arg End >>]]Append[[params]List End >>] - after delim,valstring <- [string]Get Comment DString[delims, params] {} {} - { - after <- [~]Append[after delim] - }{ - after <- "" - } - first two,rest <- [valstring]Slice[2] - If[[first two] = ["0x"]] - { - value <- From Hex@Whole Number[rest] - }{ - If[[valstring]Contains["."]] - { - value <- >]] - { - ,string begin <- [name]Slice[[[params]String Begin >>]Length] - value,after <- Parse String[string begin, params, ""] - }{ - If[[name]Starts With[[params]List Begin >>]] - { - ,list start <- [name]Slice[[[params]List Begin >>]Length] - value,after <- Parse List[list start, params, ()] - }{ - If[[[name]Slice[1]]In["-0123456789"]] - { - value,after <- Parse Number[name, params] - }{ - delims <- [[[[[[[("\n")]Append[[params]Block Begin >>]]Append[[params]Block End >>]]Append[[params]Empty Block >>]]Append[[params]Arg End >>]]Append[[params]List Delim >>]]Append[[params]List End >>]]Append[[params]List Begin >>] - afterdelim,raw before,delim,nodelim <- [name]Get Comment DString[delims, params] - - before <- Trim[raw before, "\r\n\t "] - If[[delim] = [[params]List Begin >>]] - { - value,after <- [Type Instance[before]]Parse Params[params,afterdelim] - }{ - Val[afterdelim] - { - after <- [delim]Append[~] - } - Val[nodelim] - { - after <- "" - } - If[[before] = ["Yes"]] - { - yesno <- Yes - }{ - If[[before] = ["No"]] - { - yesno <- No - }{ - If[[before] = [""]] - { - Print[[["Found "]Append[delim]]Append[" where a named pipe or literal was expected"]] - { Print[["Near: "]Append[ [afterdelim]Slice[80]]] } - }{ - If[[before]Contains[[params]Global Separator >>]] - { - parts <- [before]Split[[params]Global Separator >>] - out <- New@Global Node[Right Trim[[parts]Index[0],"\r\n\t "], Trim[[parts]Index[1], "\r\n\t "]] - }{ - out <- New@Named Pipe Node[Right Trim[before,"\r\n\t "]] - } - } - } - } - out <- New@Literal Node[yesno] - } - } - } - } - out <- New@Literal Node[value] -} - -Parse Arguments[string,params,arglist:args,after] -{ - targs <- Comment Left Trim[string, "\r\n\t ", params] - If[[targs]Starts With[[params]List Delim >>]] - { - [targs]Slice[[[params]List Delim >>]Length] {} - { - final args <- Comment Left Trim[~, "\r\n\t ", params] - } - }{ - If[[targs]Starts With[[params]Arg End >>]] - { - args <- arglist - ,after <- [targs]Slice[[[params]Arg End >>]Length] - }{ - final args <- Val[targs] - } - } - arg, after arg <- Parse Expression[final args, params] - args, after <- Parse Arguments[after arg, params, [arglist]Append[arg]] -} - -Worker or Field[name,args,params:out] -{ - get field <- [params]Get Field >> - If[[name]Ends With[get field]] - { - field <- Right Trim[[name]Slice[[[name]Length] - [[get field]Length]], "\n\r\t "] - out <- New@Field Node[field, args, No] - }{ - set field <- [params]Set Field >> - If[[name]Ends With[set field]] - { - field <- Right Trim[[name]Slice[[[name]Length] - [[set field]Length]], "\n\r\t "] - out <- New@Field Node[field, args, Yes] - }{ - out <- New@Worker Node[name, args] - } - } -} - -Prefix[string,params,name,existing args:expression,after] -{ - //Parse argument list - more args,after <- Parse Arguments[string, params, existing args] - expression <- Worker or Field[name, more args, params] -} - -Postfix or Infix[string,params:expression,after] -{ - args, after args <- Parse Arguments[string, params, ()] - delims <- [[[[[("\n")]Append[[params]Arg Begin >>]]Append[[params]Empty Block >>]]Append[[params]Block Begin >>]]Append[[params]Arg End >>]]Append[[params]List Delim >>] - aftere,before,delim <- [after args]Get Comment DString[delims, params] - { - If[[delim] = [[params]Arg Begin >>]] - { - expression, after <- Prefix[aftere, params, Trim[before,"\r\n\t "], args] - }{ - If[[delim] = [[params]Empty Block >>]] - { - after <- Val[aftere] - }{ - ,after <- [after args]Slice[[before]Length] - } - expression <- Worker or Field[Trim[before,"\r\n\t "], args, params] - } - } -} - -Parse Expression[trimmed,params:final expression,after blocks] -{ - delims <- [[[[[[[("\n")]Append[[params]Arg Begin >>]]Append[[params]Arg End >>]]Append[[params]Assign >>]]Append["\n"]]Append[[params]Empty Block >>]]Append[[params]Block End >>]]Append[[params]String Begin >>] - after, before, delim <- [trimmed]Get Comment DString[delims, params] - { - //If we find an arg begin token, we have a worker expression - If[[delim] = [[params]Arg Begin >>]] - { - maybe name <- Right Trim[before, "\r\t "] - //Prefix expressions will have the worker name before the first arg begin token - If[[maybe name] = [""]] - { - expression, after expression <- Postfix or Infix[after, params] - }{ - If[[maybe name]Contains[[params]List Delim >>]] - { - after expression <- [after literal]Append[[delim]Append[after]] - expression, after literal <- Named Pipe or Literal[Right Trim[before, "\r\n\t "], params] - }{ - expression, after expression <- Prefix[after, params, maybe name, ()] - } - } - }{ - If[[delim] = [[params]Assign >>]] - { - //Expressions starting with an assignment can be prefix, postfix or infix - //or they can be a simple literal or named pipe - assignments <- Map[[before]Split[[params]List Delim >>], ["Trim"]Set Input[1,"\n\r\t "]] - ,after blocks <- Parse Expression[Comment Left Trim[after, " \n\r\t", params], params] - { - final expression <- [~]Assignments <<[assignments] - } - }{ - //If[[delim] = [[params]String Begin >>]] - //{ - // If[[Trim[before, "\r\n\t "]] = [""]] - // { - // expression, after expression <- Named Pipe or Literal[[delim]Append[after], params] - // }{ - // after expression <- [after literal]Append[[delim]Append[after]] - // expression, after literal <- Named Pipe or Literal[Right Trim[before, "\r\n\t "], params] - // } - //}{ - // after expression <- [after literal]Append[[delim]Append[after]] - // expression, after literal <- Named Pipe or Literal[Right Trim[before, "\r\n\t "], params] - //} - expression, after expression <- Named Pipe or Literal[trimmed, params] - } - } - //Any expression can be followed by one or more blocks mapping the inputs of other expressions - //to the outputs of the current one - blocks,after blocks <- Get Expression Blocks[after expression, params, ()] - final expression <- [expression]Blocks <<[blocks] - } -} - -Worker Body[string,params,trees:trees,after end] -{ - trimmed <- Comment Left Trim[string, "\n\r\t ", params] - If[[trimmed]Starts With[[params]Block End >>]] - { - //We're done with this block, return - ,after end <- [trimmed]Slice[[[params]Block End >>]Length] - trees <- trees - }{ - expression, after expression <- Parse Expression[trimmed, params] - trees,after end <- Worker Body[after expression, params, [trees]Append[expression]] - } -} - -Process Modifiers[worker,params,modifiers:out] -{ - //Eventually this will need to be more sophisticated to handle more modifiers - trimmed <- Comment Left Trim[modifiers, "\n\r\t ", params] - If[[trimmed]Starts With[[params]Uses >>]] - { - ,after uses <- [trimmed]Slice[[[params]Uses >>]Length] - ,stores string <- [after uses]Get Comment DString["\n", params] - out <- [worker]Uses Stores <<[Map[[stores string]Split[[params]List Delim >>], ["Trim"]Set Input[1, "\r\n\t "]]] - }{ - out <- worker - } -} - -Remove Named Pipe Node[element:out] -{ - If[[Type Of[element]] = ["Named Pipe Node"]] - { - out <- [element]Name >> - }{ - If[[Type Of[element]] = ["Type Instance"]] - { - out <- [element]Params <<[ Map[[element]Params >>, "Remove Named Pipe Node"] ] - }{ - out <- element - } - } -} - -Parse Param List[text,paramlist,typelist,params:out params,out types] -{ - delims <- [[()]Append[[params]List Begin >>]]Append[[params]List Delim >>] - after,param name <-[text]Get DString[delims] {} - { - tname <- Trim[~, "\r\n\t "] - If[[tname] = [""]] - { - nextlist <- Val[paramlist] - next types <- Val[typelist] - }{ - nextlist <- [paramlist]Append[tname] - next types <- [typelist]Append[paramtype] - } - }{ - If[[~] = [[params]List Begin >>]] - { - type info,after type <- Parse List[after,params,()] - type <- [type info]Index[0] - { - If[[Type Of[~]] = ["Named Pipe Node"]] - { - before variant <- Type Instance[[type]Name >>] - }{ - before variant <- <- [type]Params <<[ Map[[type]Params >>, "Remove Named Pipe Node"] ] - } - variant <- [type info]Index[1] - { - ,warn <- If[[Type Of[~]] = ["Named Pipe Node"]] - { - before mutable,warn <- [before variant]Set Variant[[variant]Name >>] - } - Val[warn] - { - Print[[[["Warning: Invalid variant for type "]Append[[before variant]Name >>]]Append[" on input "]]Append[param name]] - before mutable <- Val[before variant] - } - }{ - before mutable <- Val[before variant] - } - [type info]Index[2] - { - paramtype <- [before mutable]Mutable? <<[ [[~]Name >>] = ["Mutable"] ] - }{ - paramtype <- Val[before mutable] - } - }{ - paramtype <- Type Instance["Any Type"] - } - [after type]Get DString[","] - { - out params,out types <- Parse Param List[~,nextlist,next types,params] - } {} {} { - out params <- Val[nextlist] - out types <- Val[next types] - } - }{ - paramtype <- Type Instance["Any Type"] - out params,out types <- Parse Param List[after,nextlist,next types,params] - } - - }{ - paramtype <- Type Instance["Any Type"] - out params <- Val[nextlist] - out types <- Val[next types] - } -} - -Worker Declaration[string,params:worker,rest,no match] -{ - ,whitespace name <- [string]Get Comment DString[[params]Arg Begin >>, params] - { - worker name <- Trim[whitespace name, "\n\r\t "] - in out <- [params]In Out Delim >> - arg end <- [params]Arg End >> - delims <- [[()]Append[in out]]Append[arg end] - after <- [~]Get Comment DString[delims, params] {} - { - arglist <- Trim[~,"\r\n\t "] - }{ - //check if there is an in/out separator - //if it isn't present, everything in the arglist is an input - If[[~] = [in out]] - { - rest <- [after]Get Comment DString[arg end, params] {} - { - outputs,output types <- Parse Param List[~, (), (), params] - } - }{ - rest <- Val[after] - outputs <- () - output types <- () - } - inputs,input types <- Parse Param List[arglist, (), (), params] - worker <- New@Parse Worker[worker name, inputs, outputs, input types, output types, 0] - } - }{}{}{ - no match <- string - } -} - -Worker Name[string,params,tree,lines:out] -{ - ,after arglist <- Worker Declaration[string, params] - { - Print[worker name] - worker name <- [~]Name >> - body text, modifiers <- [after arglist]Get Comment DString[[params]Block Begin >>, params] - modified <- Process Modifiers[~, params, modifiers] - expression trees, after body <- Worker Body[body text, params, ()] - worker <- [modified]Trees <<[expression trees] - new worker dict <- [[tree]Workers >>]Set[worker name, worker] - out <- Null[after body, params, [tree]Workers <<[new worker dict], 0] - } {} { - out <- tree - } -} - -Parse Foreign Worker[tree, string, lib, params:out] -{ - ,rest <- Worker Declaration[string, params] - { - foreign <- [~]Trees << [lib] - next <- [tree]Workers << [[[tree]Workers >>]Set[[foreign]Name >>, foreign]] - out <- Parse Foreign Worker[next, rest, lib, params] - } {} { - out <- tree - } -} - -Parse Foreign[string,params,tree,lines:out] -{ - ,after foreign <- [string]Slice[[[params]Foreign >>]Length] - [after foreign]Get Comment DString[[params]Blueprint Type Delim >>, params] - { - [~]Get Comment DString[[params]Block Begin >>, params] - { - rest, body <- [~]Get Comment DString[[params]Block End >>, params] - } - { lib <- Trim[~, "\r\n\t "] } - } - { language <- Trim[~, "\r\n\t "] } - Parse Foreign Worker[tree, body, New Foreign Lib[language, lib], params] - { - out <- Null[rest, params, ~, 0] - } -} - -Null[string,params,tree,lines:out] -{ - trimmed <- Comment Left Trim[string, " \n\r\t", params] - current line <- 0 - If[[trimmed]Starts With[ [params]Blueprint >> ]] - { - out <- PBlueprint[trimmed, params, tree, current line] - }{ - If[[trimmed]Starts With[ [params]Import >> ]] - { - out <- Parse Import[trimmed, params, tree, current line] - }{ - If[[trimmed]Starts With[ [params]Foreign >> ]] - { - out <- Parse Foreign[trimmed, params, tree, current line] - }{ - out <- Worker Name[trimmed, params, tree, current line] - } - } - } -} - -Check Worker Literals@Parse Worker[worker,program:out] -{ - If[[Type Of[[worker]Trees >>]] = ["List"]] - { - out <- [worker]Trees <<[ Map[[worker]Trees >>, ["Check Worker Literals"]Set Input[1, program]] ] - }{ - out <- worker - } -} - -Add Workers[workers,name,program:out] -{ - prog,worker <- [program]New Worker[name] - [worker]Set IO Counts[ [[[workers]Index[name]]Inputs >>]Length, [[[workers]Index[name]]Outputs >>]Length] - [workers]Next[name] - { - out <- Add Workers[workers, ~, prog] - }{ - out <- Val[prog] - } -} - -Register Workers Compile[prog, worker, name:out] -{ - //Print[["Registering "]Append[name]] - If[[ Type Of[[worker]Trees >>] ] = ["Foreign Lib"]] - { convention <- Val[[[worker]Trees >>]Language >>] } - { convention <- "rhope" } - out <- [prog]Register Worker[name, convention, [[worker]Inputs >>]Length, [[worker]Outputs >>]Length] -} - -Add Workers Compile[prog, worker, name:out] -{ - Print[["Transforming "]Append[name]] - If[[Type Of[[worker]Trees >>]] = ["Foreign Lib"]] - { - //TODO: Handle foreign func - final nworker <- [[[[[NWorker[[[worker]Trees >>]Language >>] - ]Inputs <<[ [worker]Inputs >> ] - ]Input Types <<[ [worker]Input Types >> ] - ]Outputs <<[ [worker]Outputs >> ] - ]Output Types <<[ [worker]Output Types >> ] - ]Library <<[ [[worker]Trees >>]Name >> ] - - }{ - trees, nworker, refs <- Add List to Worker[[worker]Trees >>, [[NWorker["rhope"]]Uses[[worker]Uses Stores >>]]Outputs <<[ [worker]Outputs >> ], prog, worker, Dictionary[]] - { - final nworker <- Fold[[["Add Wires Helper"]Set Input[3, worker]]Set Input[4, refs], nworker, trees] - } - - } - out <- [prog]Bind Worker[name, final nworker] -} - -Add Wires Helper[worker,node,unused,parse worker,assignments:out] -{ - out <- [node]Add Wires[worker, (), parse worker, assignments] -} - -Add Contents[parse worker,name,program:out,key] -{ - worker <- [[program]Find Worker[name]]Uses[[parse worker]Uses Stores >>] - trees, contents worker, refs <- Add List to Worker[[parse worker]Trees >>, worker, program, parse worker, Dictionary[]] - Fold[[["Add Wires Helper"]Set Input[3, parse worker]]Set Input[4, refs], contents worker, trees] - out <- [parse worker]Trees <<[trees] - key <- name -} - -Add Blueprint Field[blueprint,field,unused:out] -{ - out <- [blueprint]Add Field[[field]Name >>, [field]Type >>] -} - -Add Blueprint[prog,def:out] -{ - out <- [prog]New Blueprint[[def]Name >>] {} - { - Fold["Add Blueprint Field", ~, [def]Fields >>] - } -} - -Add Blueprint Compile[prog,def:out] -{ - out <- [prog]Bind Blueprint[[def]Name >>, Fold["Add Blueprint Field", NBlueprint[], [def]Fields >>]] -} - -_Tree to Program[parse tree,program:out] -{ - after blueprint <- Fold["Add Blueprint", program, [parse tree]Blueprints >>] - [[parse tree]Workers >>]First - { - prog <- Add Workers[[parse tree]Workers >>, ~, after blueprint] - }{ - Print["No workers!"] - } - Key Value Map[[parse tree]Workers >>, ["Add Contents"]Set Input[2, prog]] - out <- Val[prog] -} - -Tree to Program[parse tree:out] -{ - out <- _Tree to Program[parse tree, [Program[]]Add Builtins] -} - -Tree to Program Native[parse tree:out] -{ - registered <- Fold["Register Workers Compile", [Fold["Add Blueprint Compile", NProgram[], [parse tree]Blueprints >>]]Register Builtins, [parse tree]Workers >>] - out <- Fold["Add Workers Compile", registered, [parse tree]Workers >>] - { Print["Transformed AST to dataflow graph "] } -} - -Needs Imports[needs import,not imported?,name:out] -{ - If[not imported?] - { - out <- [needs import]Append[name] - }{ - out <- needs import - } -} - -Do Import[tree,file name,unused,params:out] -{ - Print[["Parsing: "]Append[file name]] - file <- >]Set[file name, No] ] -} - -Process Imports[parse tree,params:out] -{ - needs import <- Fold["Needs Imports", (), [parse tree]Imports >>] - If[[[needs import]Length] > [0]] - { - import tree <- Fold[["Do Import"]Set Input[3, params], parse tree, needs import] - out <- Process Imports[import tree, params] - }{ - out <- parse tree - } -} - -_Init Used Store[dict,store name:out] -{ - [dict]Index[store name] - { - out <- dict - }{ - Init Store[store name] - out <- [dict]Set[store name, Yes] - } -} - -_Init Used Stores[dict,worker:out] -{ - out <- Fold["_Init Used Store", dict, [worker]Uses Stores >>] -} - -Init Used Stores[parse tree,existing stores:out] -{ - out <- Fold["_Init Used Stores", existing stores, [parse tree]Workers >>] -} - -Until End[text:out] -{ - line <- Get Input[] - If[[line] = ["End"]] - { - out <- [text]Append["\n"] - }{ - out <- Until End[[[text]Append["\n"]]Append[line]] - } -} - -_REPL[params,prog,stores] -{ - line <- Get Input[] - If[[line] = ["Begin"]] - { - text <- Until End[""] - Null[text, params, New@Parse Program[], 0] - { - define tree <- Process Imports[~, params] - Init Used Stores[define tree, stores] - { _REPL[params, _Tree to Program[define tree, prog], ~] } - } - }{ - If[[line]Starts With[[params]Import >>]] - { - Parse Import[[line]Append["\n"], params, New@Parse Program[], 0] - { - import tree <- Process Imports[~, params] - Init Used Stores[import tree, stores] - { _REPL[params, _Tree to Program[import tree, prog], ~] } - } - }{ - trees <- Worker Body[[line]Append["}"], params, ()] - tree <- [New@Worker Node["Val", [()]Append[[trees]Index[0]]]]Assignments <<[("__out")] - this stores <- [[tree]Gather Stores[params, Dictionary[]]]Keys - next stores <- Fold["_Init Used Store", stores, this stores] - { - pworker <- [[New@Parse Worker["__Eval", (), ("__out"), 0]]Trees <<[[()]Append[tree]]]Uses Stores <<[this stores] - } - [[prog]Find Worker["__Eval"]]Clear - { - Add Contents[pworker, "__Eval", prog] - { - _REPL[params, prog, next stores] } } - } - } -} - -REPL[params] -{ - Print["Rhope Alpha 2\nCopyright 2008 by Michael Pavone\nEntering interactive mode\n"] - prog <- Tree to Program[Null["Val[in:out]\n{\n out <- in\n}\n__Eval[:__out]\n{\n}\n", params, New@Parse Program[], 0]] - _REPL[params, prog, Dictionary[]] -} - -Add If Store[stores,name,params:out] -{ - If[[name]Contains[[params]Global Separator >>]] - { - parts <- [name]Split[[params]Global Separator >>] - out <- [stores]Set[[parts]Index[0], Yes] - }{ - out <- stores - } -} - -Param Gather Stores[stores,node,params:out] -{ - out <- [node]Gather Stores[params, stores] -} - -Gather Stores@Named Pipe Node[node,params,stores:out] -{ - out <- Fold[["Add If Store"]Set Input[2, params], stores, [node]Assignments >>] -} - -Gather Stores@Global Node[node,params,stores:out] -{ - out <- [stores]Set[[node]Store >>, Yes] -} - -Gather Stores@Worker Node[node,params,stores:out] -{ - //TODO: Handle blocks - store list <- Fold[["Param Gather Stores"]Set Input[2, params], stores, [node]Params >>] - out <- Fold[["Add If Store"]Set Input[2, params], store list, [node]Assignments >>] -} - -Gather Stores@Field Node[node,params,stores:out] -{ - //TODO: Handle blocks - store list <- Fold[["Param Gather Stores"]Set Input[2, params], stores, [node]Params >>] - out <- Fold[["Add If Store"]Set Input[2, params], store list, [node]Assignments >>] -} - -Gather Stores@Literal Node[node,params,stores:out] -{ - out <- Fold[["Add If Store"]Set Input[2, params], stores, [node]Assignments >>] -} -