Skip to content

Commit

Permalink
Add built-ins for reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
MatwayBurkow committed Jan 31, 2023
1 parent 90b983b commit 10f4ccd
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 19 deletions.
119 changes: 109 additions & 10 deletions builtinImpl.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# By contributing to the repository, contributors acknowledge that ownership of their work transfers to the owner.

"Array" use
"HashTable" use
"Owner" use
"String" use
"algorithm" use
Expand All @@ -15,7 +14,6 @@

"Block" use
"Var" use
"astNodeType" use
"codeNode" use
"debugWriter" use
"declarations" use
Expand All @@ -28,10 +26,6 @@
"staticCall" use
"variable" use

debugMemory [
"memory.getMemoryMetrics" use
] [] uif

declareBuiltin: [
virtual declareBuiltinName:;
virtual declareBuiltinBody:;
Expand All @@ -45,8 +39,6 @@ declareBuiltin: [
];

mplBuiltinProcessAtList: [
refToStruct: @processor @block pop;
refToIndex: @processor @block pop;
compileOnce

result: RefToVar;
Expand Down Expand Up @@ -111,6 +103,18 @@ mplBuiltinProcessAtList: [
result
];

mplBuiltinProcessAtListKeyObject: [
refToStruct: @processor @block pop;
refToIndex: @processor @block pop;
mplBuiltinProcessAtList
];

mplBuiltinProcessAtListObjectKey: [
refToIndex: @processor @block pop;
refToStruct: @processor @block pop;
mplBuiltinProcessAtList
];

mplNumberBinaryOp: [
exValidator:;
getResultType:;
Expand Down Expand Up @@ -433,7 +437,7 @@ staticityOfBinResult: [
];

[
field: mplBuiltinProcessAtList;
field: mplBuiltinProcessAtListKeyObject;
processor compilable [
field @processor @block setRef
] when
Expand Down Expand Up @@ -556,7 +560,7 @@ staticityOfBinResult: [
] "mplBuiltinGreater" @declareBuiltin ucall

[
field: mplBuiltinProcessAtList;
field: mplBuiltinProcessAtListKeyObject;
processor compilable [
field isVirtual [@field @processor @block makeVirtualVarReal @field set] when
@field @processor @block derefAndPush
Expand Down Expand Up @@ -900,6 +904,13 @@ staticityOfBinResult: [
] [ceil] [x:;] mplNumberBuiltinOp
] "mplBuiltinCeil" @declareBuiltin ucall

[
refToVar: @processor @block pop;
processor compilable [
refToVar getVar.data.getTag VarCode = makeValuePair VarCond @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] when
] "mplBuiltinCodeQuestion" @declareBuiltin ucall

[
(
[processor compilable]
Expand All @@ -917,6 +928,13 @@ staticityOfBinResult: [
) sequence
] "mplBuiltinCodeRef" @declareBuiltin ucall

[
refToVar: @processor @block pop;
processor compilable [
refToVar getVar.data.getTag VarImport = makeValuePair VarCond @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] when
] "mplBuiltinCodeRefQuestion" @declareBuiltin ucall

[
TRUE dynamic @block.@nodeCompileOnce set
] "mplBuiltinCompileOnce" @declareBuiltin ucall
Expand Down Expand Up @@ -1087,6 +1105,54 @@ staticityOfBinResult: [
) sequence
] "mplBuiltinFieldIndex" @declareBuiltin ucall

[
refToCount: @processor @block pop;
refToVar: @processor @block pop;
processor compilable [
varCount: refToCount getVar;
varCount.data.getTag VarInt32 = ~ ["index must be Int32" @processor block compilerError] when
processor compilable [
refToCount staticityOfVar Dynamic > ~ ["index must be static" @processor block compilerError] when
processor compilable [
count: VarInt32 varCount.data.get.end 0 cast;
var: refToVar getVar;
var.data.getTag VarStruct = ~ ["not a combined" @processor block compilerError] when
processor compilable [
struct: VarStruct var.data.get.get;
count 0 < [count struct.fields.getSize < ~] || ["index is out of bounds" @processor block compilerError] when
processor compilable [
count struct.fields.at.refToVar getVar.data.getTag VarRef = makeValuePair VarCond @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] when
] when
] when
] when
] when
] "mplBuiltinFieldIsRef" @declareBuiltin ucall

[
refToCount: @processor @block pop;
refToVar: @processor @block pop;
processor compilable [
varCount: refToCount getVar;
varCount.data.getTag VarInt32 = ~ ["index must be Int32" @processor block compilerError] when
processor compilable [
refToCount staticityOfVar Dynamic > ~ ["index must be static" @processor block compilerError] when
processor compilable [
count: VarInt32 varCount.data.get.end 0 cast;
var: refToVar getVar;
var.data.getTag VarStruct = ~ ["not a combined" @processor block compilerError] when
processor compilable [
struct: VarStruct var.data.get.get;
count 0 < [count struct.fields.getSize < ~] || ["index is out of bounds" @processor block compilerError] when
processor compilable [
count struct.fields.at.refToVar isVirtual makeValuePair VarCond @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] when
] when
] when
] when
] when
] "mplBuiltinFieldIsVirtual" @declareBuiltin ucall

[
refToCount: @processor @block pop;
refToVar: @processor @block pop;
Expand All @@ -1111,6 +1177,21 @@ staticityOfBinResult: [
] when
] "mplBuiltinFieldName" @declareBuiltin ucall

[
field: mplBuiltinProcessAtListObjectKey;
processor compilable [
field isVirtual [@field @processor @block makeVirtualVarReal @field set] when
@field @processor @block derefAndPush
] when
] "mplBuiltinFieldRead" @declareBuiltin ucall

[
field: mplBuiltinProcessAtListObjectKey;
processor compilable [
field @processor @block setRef
] when
] "mplBuiltinFieldWrite" @declareBuiltin ucall

[
TRUE dynamic @processor.@usedFloatBuiltins set
[a:; a getVar.data.getTag VarReal32 = ["@llvm.floor.f32" makeStringView]["@llvm.floor.f64" makeStringView] if
Expand Down Expand Up @@ -1752,6 +1833,17 @@ staticityOfBinResult: [
] when
] "mplBuiltinUif" @declareBuiltin ucall

[
refToVar: @processor @block pop;
processor compilable [
refToVar getVar.data.getTag VarImport = ~ [refToVar getVar.data.getTag VarString = ~] && [
TRUE @refToVar.setMutable
] when

refToVar @block push
] when
] "mplBuiltinUnconst" @declareBuiltin ucall

FindInPathResult: {
NO_FILE: [0];
FILE_WITH_ERROR: [1];
Expand Down Expand Up @@ -1982,6 +2074,13 @@ tryFindInPath: [
TRUE @block.@nextLabelIsVirtual set
] "mplBuiltinVirtual" @declareBuiltin ucall

[
refToVar: @processor @block pop;
processor compilable [
refToVar isVirtual makeValuePair VarCond @processor @block createVariable Static @processor @block makeStaticity @processor @block createPlainIR @block push
] when
] "mplBuiltinVirtualQuestion" @declareBuiltin ucall

[
VarCond VarNatX 1 + [a2:; a1:; "xor" makeStringView] [xor] [new] [y:; x:;] mplNumberBinaryOp
] "mplBuiltinXor" @declareBuiltin ucall
Expand Down
22 changes: 13 additions & 9 deletions builtins.mpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
# It is forbidden to use the content or any part of it for any purpose without explicit permission from the owner.
# By contributing to the repository, contributors acknowledge that ownership of their work transfers to the owner.

"String" use
"String" use
"control" use
"conventions" use

"Block" use
"MplFile" use
"Var" use
"Block" use
"Var" use
"builtinImpl" use
"codeNode" use
"declarations" use
"codeNode" use
"defaultImpl" use
"processor" use
"variable" use
"processor" use

builtins: (
{name: "!" ; impl: @mplBuiltinExclamation ;}
Expand All @@ -44,7 +40,9 @@ builtins: (
{name: "callField" ; impl: @mplBuiltinCallField ;}
{name: "cast" ; impl: @mplBuiltinCast ;}
{name: "ceil" ; impl: @mplBuiltinCeil ;}
{name: "code?" ; impl: @mplBuiltinCodeQuestion ;}
{name: "codeRef" ; impl: @mplBuiltinCodeRef ;}
{name: "codeRef?" ; impl: @mplBuiltinCodeRefQuestion ;}
{name: "compileOnce" ; impl: @mplBuiltinCompileOnce ;}
{name: "const" ; impl: @mplBuiltinConst ;}
{name: "cos" ; impl: @mplBuiltinCos ;}
Expand All @@ -55,7 +53,11 @@ builtins: (
{name: "failProc" ; impl: @mplBuiltinFailProc ;}
{name: "fieldCount" ; impl: @mplBuiltinFieldCount ;}
{name: "fieldIndex" ; impl: @mplBuiltinFieldIndex ;}
{name: "fieldIsRef" ; impl: @mplBuiltinFieldIsRef ;}
{name: "fieldIsVirtual" ; impl: @mplBuiltinFieldIsVirtual ;}
{name: "fieldName" ; impl: @mplBuiltinFieldName ;}
{name: "fieldRead" ; impl: @mplBuiltinFieldRead ;}
{name: "fieldWrite" ; impl: @mplBuiltinFieldWrite ;}
{name: "floor" ; impl: @mplBuiltinFloor ;}
{name: "getCallTrace" ; impl: @mplBuiltinGetCallTrace ;}
{name: "has" ; impl: @mplBuiltinHas ;}
Expand Down Expand Up @@ -101,8 +103,10 @@ builtins: (
{name: "textSplit" ; impl: @mplBuiltinTextSplit ;}
{name: "ucall" ; impl: @mplBuiltinUcall ;}
{name: "uif" ; impl: @mplBuiltinUif ;}
{name: "unconst" ; impl: @mplBuiltinUnconst ;}
{name: "use" ; impl: @mplBuiltinUse ;}
{name: "virtual" ; impl: @mplBuiltinVirtual ;}
{name: "virtual?" ; impl: @mplBuiltinVirtualQuestion ;}
{name: "xor" ; impl: @mplBuiltinXor ;}
{name: "~" ; impl: @mplBuiltinNot ;}
);
Expand Down

0 comments on commit 10f4ccd

Please sign in to comment.