-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Discussion] [Bugfix] casts, automatic toString()
, null
, etc.
#56
base: master
Are you sure you want to change the base?
[Discussion] [Bugfix] casts, automatic toString()
, null
, etc.
#56
Conversation
Thank you for all this work! I need time to contemplate your commits, so i must ask for patience. Two months ago i began rewriting the code generator, type resolver and interpreter from scratch (branch newCompiler), so please don't invest too much effort into syntactical details. Es würde mich freuen, wenn ich das weitere Vorgehen mal telefonisch mit Ihnen besprechen könnte. Ich will mich aber nicht aufdrängen - nur, wenn Ihnen das Recht ist. Bitte schreiben Sie mir ggf. einfach per Mail, wann Sie Zeit hätten: asv.pabst at gmail dot com. Viele Grüße Martin Pabst |
I noticed several bugs regarding casts, automatic
toString()
andnull
, i.e. behaviours that differ from the behaviour in Java.I'm not sure, whether some of the differences are on purpose (it's not a bug, it's a feature).
If so, I suggest to add these differences to the page
LernJ vs. Java: Unterschiede
in the documentation instead.As the problems are unfortunately very closely interwoven, there were quite a lot of changes in the interpreter and the compiler nescessary.
They are primarily meant as suggestions to be discussed. This is why I created this pull request only as draft.
While some changes (like b5c3a53) can probably be adopted directly, you may have better suggestions for other problems.
In the following I listed the bugs and how I fixed them in a table:
Bugs
1d
and1f
areint
s1
1.5
==
instead of=
) in line 917 of the lexer.double
s andfloat
s3.0
3
double
s orfloat
s, that are ∈ ℤ, they end on.0
. Since javascript only knows onenumber
type, this has to be done manually.floatToString()
valueToString()
inPrimitiveType
called byensureAutomaticToString
inCodeGenerator.ts
is overwritten inFloatPrimitiveType
andDoublePrimitiveType
, so that it callsfloatToString()
.toString()
-methods ofDoubleClass
andFloatClass
toString()
-methods ofDoubleClass
andFloatClass
Integer
tolong
Der Datentyp Integer kann (zumindest durch casting) nicht in den Datentyp long umgewandelt werden.
1
longPrimitiveType
is missing inunboxableAs
ofIntegerClass
longPrimitiveType
"Long"
to thecanCastToMap
ofIntPrimitiveType
(Although in Java it is not possible to cast anint
to aLong
, I think this makes sense, as there is no real difference, since everything is stored as javascript-number internally, and in the Online IDE it is also possible to cast afloat
to aDouble
)null
null
PrintManager
prints an empty line, whentext == null
.Interpreter
calls thePrintManager
, it checks, whetherprintln
is called without parameters, than stillnull
is passed, or if it is called withnull
as parameter, than"null"
is passed.Fehler: Aufruf der Methode toString des null-Objekts
String
in the current version is done ingetToStringStatement()
, which inserts a call of thetoString()
-method of the respective class.ensureAutomaticToString()
is a bit more complex, but considers this and other cases. Instead of inserting an call of thetoString()
of the respective class, a call of an artifical method is inserted, which is static and first catches the case, that the parameter isnull
(then"null"
is returned), before invoking the actualtoString()
method of the respective class.0
nullnull
null + null = 0
(don't ask me why). It has to be fixed in thecompute()
method ofStringPrimitiveType
null
, now"null"
is used instead of it. Additionally a string now only allows concatenation with other strings (5c2f340, 97c862b). If one of the operands is no string, it is automatically converted to a string inensureAutomaticToString()
.null
Die Operation == ist für die Operanden der Typen null und Circle nicht definiert.
true
NullType
allows no operations at all, sincegetResultType()
always returnsnull
.NullType
now allows the operationsTokenType.equal
andTokenType.notEqual
, if thesecondOperandType
allows this operation with theNullType
(so if the operation works the other way around). For both operationscompute()
was implemented.null
Object to a primitive typetrue
without inserting statements. Thus, at run time it won't be checked, whether the unboxed value isnull
.UnboxableKlass
. This class implementscastTo()
andcanCastTo()
.castTo()
will throw an error, when the castedvalue
isnull
.Interpreter
will catch this error and display it.(Rectangle)
String
(due to lines 625-627 inKlass.canCastTo()
) and everything can be automatically casted to a string (due to line 847-852 inensureAutomaticCasting
in theCodeGenerator.ts
). I'm not sure whether this is an feature or a bug. However it don't think it makes sense, sincetoString()
or+""
insteadString
(in Klass as well as in the primitive types)ensureAutomaticToString()
, that takes care of the auto-conversion when callingprint
or concatenating with aString
CodeGenerator.ts
, that allowed auto-casting to aString
String
something likesetFillColor(Color.red)
wouldn't work anymore, I added corresponding methods that accept aColor
-object as parameter.You can try out the fixed version in my embedded IDE: 🡭 Start in IDE
Please comment on which of these changes make sense and for which a better solution should be found.