From 10f4ccd06d8d33009600b93f5f8aa5690a28d34d Mon Sep 17 00:00:00 2001 From: Matway Burkow <2745204+MatwayBurkow@users.noreply.github.com> Date: Tue, 31 Jan 2023 22:13:48 +0300 Subject: [PATCH] Add built-ins for reflection --- builtinImpl.mpl | 119 ++++++++++++++++++++++++++++++++++++++++++++---- builtins.mpl | 22 +++++---- 2 files changed, 122 insertions(+), 19 deletions(-) diff --git a/builtinImpl.mpl b/builtinImpl.mpl index c7f146d..f89724d 100644 --- a/builtinImpl.mpl +++ b/builtinImpl.mpl @@ -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 @@ -15,7 +14,6 @@ "Block" use "Var" use -"astNodeType" use "codeNode" use "debugWriter" use "declarations" use @@ -28,10 +26,6 @@ "staticCall" use "variable" use -debugMemory [ - "memory.getMemoryMetrics" use -] [] uif - declareBuiltin: [ virtual declareBuiltinName:; virtual declareBuiltinBody:; @@ -45,8 +39,6 @@ declareBuiltin: [ ]; mplBuiltinProcessAtList: [ - refToStruct: @processor @block pop; - refToIndex: @processor @block pop; compileOnce result: RefToVar; @@ -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:; @@ -433,7 +437,7 @@ staticityOfBinResult: [ ]; [ - field: mplBuiltinProcessAtList; + field: mplBuiltinProcessAtListKeyObject; processor compilable [ field @processor @block setRef ] when @@ -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 @@ -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] @@ -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 @@ -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; @@ -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 @@ -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]; @@ -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 diff --git a/builtins.mpl b/builtins.mpl index 26e6d09..8b04ca2 100644 --- a/builtins.mpl +++ b/builtins.mpl @@ -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 ;} @@ -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 ;} @@ -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 ;} @@ -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 ;} );