mirror of https://github.com/tbklang/tlang.git
🧠️ Feature: Universal coercion and type enforcer #115
Labels
No Label
dependency
emit
hashmapper
lebanonmapper
lexer
meta
needsfix
parser
qol
question
resolution
typing
No Milestone
No project
No Assignees
1 Participants
Notifications
Total Time Spent: 9 hours 34 minutes
Due Date
deavmi
9 hours 34 minutes
Blocks
Depends on
#61 Type checking everything!
tlang/tlang
You do not have permission to read 3 dependencies
Reference: tlang/tlang#115
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What is it?
A few things:
LiteralValue
instructions, anything else requires acast
if size is big->small)isSameType(Type t1, Type t2)
)typeEnforce(Type t1, Value v2, bool coerce = false)
that can be used anywhere in the typechecker to do these checks (and cast towards the coerced type by generating aCastedValueInstruction
)Things to change:
attemptCoercion(Type, Value)
only throwsCoercionException
Things to add:
TypeCheckerException
,CoercionException
, which is thrown whenattemptCOercion(Type, Value)
failsUniversal coercionto 🧠️ Feature: Universal coercionShould take in arguments rather
Type t1, Value v2
.Updated to this now
Work on this is going well, I am writing a unit test right now to test how I would use it on practice, I am then going to update the
attemptCoercion
method to effectively support all sorts of instructions (basically any Value instruction) and then use coercion rules to try and coerce.Mmh, do we want to auto coerce a lot of these things?
Look, it makes sense for the
LiteralValue
instructions as we then leave it really up to the C emitter at the end to do the right thing, however, and I am spit-balling here. If we were to coerce the value as shown below (in the return statement):Then we would automatically be updating the type of the
value
instruction as part of theReturnInstruction
, it doesn't necessarily make sense to do that automatically - at all because it can result in the wrong code if we were to be emitting for something like JVM which really cares, instead of theDGen
emitter which relies on C in that sense.Therefore I think we should not add any of that sort of funcitonality to
attemptCoerce(Type, Value)
, rather we should leave it to require a cast as normal as atleast then we could control conversion in theCodeEmitter
irrespective of what emitter was usedDGen
(C emitter) or any future JVM emitter for example.However, what we should do is, in the case of literals and so on, is use
typeEnforce(Type, Value, true)
where we can - such as in return statements - but only forLiteralValue
. I think it is good to require a cast, remember if we were to do proper auto coercion for any instruction it would actually require an instruction replace (with embed) - we don't want to do that - that was never the goal.I have re-defined the TODOs for this issue to reflect this now 👍
As of commit
332413279b26a840e1fc5a93af5ba77f4ace68eb
we have what seems to be a workingtypeEnforce(Type, Value, bool coerce = false)
method ✅🧠️ Feature: Universal coercionto 🧠️ Feature: Universal coercion and type enforcertypeEnforcer()
everywhereBump, this needs to be looked into and well worked on more. I am sure, from when I last looked at it, that the type enforcer function was ready to be used (as in already implemented) just needed to actually use it and then test it.
Updated to latest base of
vardec)varass_dependency
✅Working on this again now.
Added size-based test when both the to-type and provided-type are numerical types.
Complete with unit tests for positive and negative cases.
So far only Variable declaration is calling this.
Unit tests (not test cases) seem to be failing:
Could be a case of a tets case that should now pass based on coercion.
Well from the test case:
THis is no longer true, it IS allowed - I am legit trying to implement that. Lmao
Fixed now ✅
Working on adding support for standalone variable assignments now. That is to say, migrating to using
typeEnforce()
Done coding it ✅ - CI is good!
Aight, will take little break now but gonna keep grinding this!
Migration to
typeEnforce()
v1Basics
These don't need much thought as to how we want it done in TLang.
More thought
biggerOfTheTwo(Integer integralType1, Integer integralType2)
to determine the bigger one and then try coerce the smaller one to it!Questions ❓
We can emit stuff and hope that C then obviously provides widening and shortening but if we were to emit our own language that was a lowermlevel IR or VM code then we'd need to make those actions explicit. This isn't necessarily hard buttypeEnforce()
would need a third parameter (of typeref Instruction
) which could dump the widening/shortening/conversion instructio which we need to save to the stack-queue too during code generation.Another way we could make it explicit is by, instead of updating the types we maketypeEnforce()
NOT callsetType()
but rather emit aCastInstruction
.Completed with #137
Updates for
typeEnforce()
v1canCoerceStackArray(Type, Type, Type)
- as required for function callsIt would seem one of the things we will need to do is to put code for array coercion to ptr and stack array to array coercion in our
attemptCoercion()
somewhere before we can do functions as it relies on those checks as part of its oldisSameType
,canStack...()
code - the speghetti we are removing! :) 🍝Synced with
vardec_varass_depencency
, no merge conflicts and CI/CD is all green ✅I will try finish this, this coming week - this is a very important feature and has already been added to the documentation so it MUST work.
It is just array stuff that remains (which needs to be incoprorated into
typeEnforce()
's definition and also used in those places and also BinaryOp I believe.Lunch time! 🥪
Working on adding pointer coercion under certain circumstances...
BinaryOperator
expressions indoTypeCheck()
.I am almost done (very sure I am done with actually) the changes I needed in #140 to continue (was emitted code getting ih the way).
See #140 for more information.
Working on the binary operator type enforcement again now.
Function call stack array coercion seems to be working
Completed #113 ✅
Just merged #143 into the
vardec_varass_dependency
, upstreaming intofeatuire/universal_coercion
...Things are failing! ❗ ❗ ❗
CI/CD and test cases
Failing test cases
simple_function_recursion_factorial.t
[ERROR] Function 'factorial' declared with return type does not contain a return statement
Syncing now...
We now are getting a different error, type checking related probably (see https://github.com/tbklang/tlang/actions/runs/5568915900/jobs/10171880242?pr=9)
Doesn't seem to be type checker related 🤔
Fixed it, was related to
ReturnStmt
checking inParser
(the last unit test)CI/CD is ✅
So unit tests fine. Also fixed testcase
simple_functions.t
(upstreamed both this and the above unit test changes fromvardec_varass_dependency
) which had a missingreturn
statement in thebanana
function.I LIE! It is not all done, still got some failing tests.
Found another one, fixed it now. Same story - missing
return
statements.This time it's all fixed for real ✅ ✅ ✅ ✅ ✅
I should finish this up this week and just go ahead and merge it. Any things requiring coercion can be fixed later, it's a very effortless migration.
I'm going to go ahead and merge this
Merged, awaiting CI/CD ...
All passed ✅