Mercurial > repos > rhope
diff cbackend.rhope @ 36:495dddadd058
User defined types work in the compiler now
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 03 Oct 2009 03:18:15 -0400 |
parents | 3498713c3dc9 |
children | 640f541e9116 |
line wrap: on
line diff
--- a/cbackend.rhope Wed Sep 30 23:55:04 2009 -0400 +++ b/cbackend.rhope Sat Oct 03 03:18:15 2009 -0400 @@ -65,7 +65,7 @@ C Field Registry[:out] { - out <- [[Build["C Field Registry"]]Lookup <<[New@Dictionary[]]]Next ID<<[0] + out <- [[Build["C Field Registry"]]Lookup <<[New@Dictionary[]]]Next ID<<[1] } @@ -117,31 +117,31 @@ 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", field reg, [ctype]Fields >>] + out <- Fold["_Register Field C", field reg, [ctype]Fields >>] } -Rhope Type to C[typename,naked:out] +Rhope Type to C[typename:out] { If[[typename] = ["Any Type"]] { out <- "struct object *" }{ - ctype <- ["t_"]Append[Escape Rhope Name[typename]] - If[naked] - { - out <- Val[ctype] - }{ - out <- [ctype]Append[" *"] - } + out <- [["t_"]Append[Escape Rhope Name[typename]]]Append[" *"] } } _Type Def C Type[text,field:out] { name <- [field]Index[0] - type <- ["\n\t"]Append[Rhope Type to C[[field]Index[1], No]] + type <- ["\n\t"]Append[Rhope Type to C[[field]Index[1]]] out <- [[[[text]Append[type]]Append[" "]]Append[Escape Rhope Name[name]]]Append[";"] } @@ -155,9 +155,15 @@ 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] { - //TODO: Handle function pointers for build/copy/destroy funcs start <- [["\tbp = register_type_byid(" ]Append[id] ]Append[ @@ -165,15 +171,15 @@ ]Append[ ["t_"]Append[Escape Rhope Name[ [ctype]Name >> ]]] ]Append[ - ["), "]Append[ + ["), (special_func)"]Append[ [ [[[[Escape Rhope Name NU[[ctype]Init >>] - ]Append[", "] + ]Append[", (special_func)"] ]Append[Escape Rhope Name NU[[ctype]Copy >> ]] - ]Append[", "] + ]Append[", (special_func)"] ]Append[Escape Rhope Name NU[[ctype]Cleanup >>]] ]Append[");"]]] ] - out <- Fold[[["_Type Init C"]Set Input[0, [ctype]Name >>]]Set Input[1, method reg], start, [ctype]Methods >>] + out <- 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 @@ -209,7 +215,7 @@ _Type Defs C[text,def:out] { - out <- [[text]Append["\n\n"]]Append[[def]Type Def] + out <- [[text]Append[[def]Type Def]]Append["\n\n"] } Type Defs@C Type Registry[reg:out] @@ -219,7 +225,7 @@ _Type Inits C[reg,method reg,field reg,text,def,name:out] { - out <- [[text]Append["\n\n"]]Append[ [def]Type Init[[reg]Type ID[name], method reg, field reg] ] + 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] @@ -255,6 +261,7 @@ Statements Method Registry Field Registry + Type Registry Constants Input Types Output Types @@ -267,7 +274,7 @@ C Function With Registry[name,inputs,outputs,convention,registry,field reg:out] { - out <- [[[[[[[[[[[Build["C Function"] + out <- [[[[[[[[[[[[Build["C Function"] ]Name <<[name] ]Inputs <<[inputs] ]Outputs <<[outputs] @@ -276,6 +283,7 @@ ]Statements <<[()] ]Method Registry <<[registry] ]Field Registry <<[field reg] + ]Type Registry <<[C Type Registry[]] ]Constants <<[New@Dictionary[]] ]Input Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), inputs] ] ]Output Types <<[ Fold[["Append"]Set Input[1, "Any Type"], (), outputs] ] @@ -295,7 +303,6 @@ { Print["Register Constant"] Print[name] - Print[constant] out <- [func]Constants <<[ [[func]Constants >>]Set[name, constant] ] { Print["Got register constant output"] } } @@ -351,7 +358,7 @@ out <- [func]Add Statement[[[dest]Append[" = "]]Append[source]] } -AddRef@C Function[func,psource,pdest:out] +Do AddRef@C Function[func,psource,pdest:out] { source <- [psource]Make Op[func] dest <- [pdest]Make Op[func] @@ -384,10 +391,34 @@ Field Result@C Function[func,var,field:out] { - out <- [[var]Append["->"]]Append[field] + as op <- [var]Make Op[func] + If[[[Type Of[var]] = ["String"]] And[ [[func]Convention >>] = ["rhope"]] ] + { + [[func]Inputs >>]Find[var] + { + type <- [[func]Input Types >>]Index[~] + If[[type] = ["Any Type"]] + { + rvar <- Val[as op] + }{ + rvar <- [[[["(("]Append[ Rhope Type to C[type] ]]Append[")("]]Append[as op]]Append["))"] + } + }{ + rvar <- Val[as op] + } + }{ + rvar <- Val[as op] + } + out <- [[rvar]Append["->"]]Append[Escape Rhope Name[field]] } -Get Field@C Function[func,var,field:out,result op] +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] @@ -398,6 +429,12 @@ out <- [func]Add Statement[ [[[var]Append["->"]]Append[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[")"]] ] +} + _Function Arg C[func,val,inputnum:out] { out <- [func]Add Statement[ @@ -524,11 +561,11 @@ } _Output Defs C[string,varname,index,func:out] { - out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[[[func]Output Types >>]Index[index], No]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"] + out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[[[func]Output Types >>]Index[index]]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"] } _Var Defs C[string,type,varname:out] { - out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type, No]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"] + out <- [[[string]Append[ ["\t"]Append[Rhope Type to C[type]] ]]Append[Escape Rhope Name[varname]]]Append[";\n"] } @@ -560,19 +597,39 @@ out <- [localtype]Append[proto] } +_Proto Input[list,input,index,types:out] +{ + Print[["_Proto Input: "]Append[input]] + { Print[[types]Index[index]] } + out <- [list]Append[ [[Rhope Type to C[[types]Index[index]]]Append[" "]]Append[Escape Rhope Name[input]] ] + { Pretty Print[~, ""] } +} + Naked Proto@C Function[func:out] { + Print[["Naked Proto: "]Append[ [func]Name >>] ] [[func]Output Types >>]Index[0] { - outtype <- [Rhope Type to C[~, Yes]]Append[" "] + outtype <- [Rhope Type to C[~]]Append[" "] }{ outtype <- "void " } out <- [[[[outtype ]Append[ Escape Rhope Name NU[[func]Name >>]] ]Append["("] - ]Append[ [[func]Input Types >>]Join[", "] ] + ]Append[ [Fold[["_Proto Input"]Set Input[3, [func]Input Types >>], (), [func]Inputs >>]]Join[", "] ] ]Append[")"] + { Print[~] } +} + +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[")"] ] ] + } } Text@C Function[func:out] @@ -580,7 +637,7 @@ If[ [[func]Convention >>] = ["rhope"] ] { cname <- Escape Rhope Name[[func]Name >>] - If[ [[[func]Variables >>]Length] = [0] ] + If[ [ [[[func]Variables >>]Length]+[[[func]Outputs >>]Length] ] = [0] ] { out <- [[[[[["FuncNoLocals(" ]Append[cname] @@ -663,9 +720,9 @@ } } -_Text C Program[text,func:out] +_Text C Program[text,func,type reg:out] { - out <- [text]Append[[[func]Text]Append["\n\n"]] + out <- [text]Append[[[ [func]Type Registry <<[type reg] ]Text]Append["\n\n"]] } Combine Consts[consts,func:out] @@ -678,24 +735,37 @@ out <- [text]Append[ [["object * _const_"]Append[Escape Rhope Name[name]]]Append[";\n"] ] } -_Set Consts C Program[text,value,name:out] +_Set Consts C Program[text,value,name,type reg:out] { //TODO: Support more constant types - out <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Int32("]]Append[value]]Append[");\n"]] + valtype <- Type Of[value] + [("Int32","Whole Number")]Find[valtype] + { + out <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Int32("]]Append[value]]Append[");\n"]] + }{ + If[[valtype] = ["Type Literal"]] + { + //TODO: Support parametric types + typeid <- [type reg]Type ID[[value]Name >>] + out <- [text]Append[[[[["\t_const_"]Append[Escape Rhope Name[name]]]Append[" = make_Blueprint("]]Append[typeid]]Append[");\n"]] + } + } } Text@C Program[program:out] { + Print["Text@C Program"] constants <- Fold["Combine Consts", New@Dictionary[], [program]Functions >>] headers <- "#include <stdio.h> #include \"builtin.h\" #include \"object.h\" #include \"context.h\" #include \"func.h\" -#include \"integer.h\"\n\n" +#include \"integer.h\" +#include \"blueprint.h\"\n\n" out <- [[[[[[headers ]Append[[[program]Type Registry >>]Type Defs] - ]Append[Fold["_Text C Program", + ]Append[Fold[["_Text C Program"]Set Input[2, [program]Type Registry >>], Fold["_Consts C Program", Fold["_Defs C Program", "", [program]Functions >>], constants @@ -705,9 +775,10 @@ returntype ret; calldata *cdata; context * ct; + blueprint * bp; register_builtin_types();\n\n"] ]Append[ [[program]Type Registry >>]Type Inits[[program]Method Registry >>, [program]Field Registry >>] ] - ]Append[Fold["_Set Consts C Program", "", constants]] + ]Append[Fold[["_Set Consts C Program"]Set Input[3, [program]Type Registry >>], "", constants]] ]Append[" ct = new_context(); cdata = alloc_cdata(ct, 0);