Monkey-C grammar questions

I have started to develop a couple of apps to 920xt, and in that connection I have a couple of questions to the Monkey C grammar. Bear over with me if the answers can be found in the documentation - I just haven't found them :-)

  • Does "this", "self", "it", whatever exist? I.e. a reference to the "current object context" for functions inside a class declaration. These can be extremely useful and help avoid creation of many new objects. For now, I have used "var a = new A(); a.self = a;"... but that isn't exactly pretty and is very error prone.
  • “var x = new {};” is not accepted by the compiler as otherwise stated in the documentation. "var x = {};" is accepted, so no real sweat...
  • "static" is allowed for class and enum declarations, but what are this supposed to mean?
  • "C has :function" fails where C is a class (and work properly for object of C). Any particular reasons why one cannot test the presence of a method or variable in a class without instantiating it?
  • "expr?expr:expr" construct is not mentioned in docs, but seems to work (with a very high preceedence).
  • "f().a = 0;" fails with a syntax error - i.e. "f()" is not allowed in l-value expressions. Why? "f()" could return an object, so "f().a" could be a perfectly valid l-value.
  • "a+2;" (as a statement) fails, but "(a+2);" does not. Why?


Most - if not all - of these questions could be answered if the documentation includes a reference section with the complete grammar (in EBNF?), and a list of all built-in operations and the valid operands... Any chance of getting that?
  • Former Member
    Former Member over 10 years ago
    Can probably only shed some light on 1. self does work eg
    var value;

    function same(value){
    self.value = value;
    }

    You can also pass instance to delegate etc..
    Ui.pushView(new Rez.Menus.MainMenu(self), new MainMenuDelegate(self), Ui.SLIDE_UP);

    Nice to know 5 works. I hadn't bother to test it.
  • Yup.... "self" worked...

    // Tests use of self in classes - see Test class
    function testSelf() {
    var o = new Test(10);
    assertEquals(o, o.getSelf());
    }

    class Test {
    var s;
    function initialize(i) {
    s = i;
    }

    function getSelf() {
    return self;
    }
    }
  • Also, what are are formats and ranges for the different number types? Just like in Java or...?
  • Former Member
    Former Member over 10 years ago
    One look at your coding tells me that its the student answering the teacher :) but look to pages 20-22 of the programmers guide.
  • I have started to develop a couple of apps to 920xt, and in that connection I have a couple of questions to the Monkey C grammar. Bear over with me if the answers can be found in the documentation - I just haven't found them :-)

    • Does "this", "self", "it", whatever exist? I.e. a reference to the "current object context" for functions inside a class declaration. These can be extremely useful and help avoid creation of many new objects. For now, I have used "var a = new A(); a.self = a;"... but that isn't exactly pretty and is very error prone.
    • “var x = new {};” is not accepted by the compiler as otherwise stated in the documentation. "var x = {};" is accepted, so no real sweat...
    • "static" is allowed for class and enum declarations, but what are this supposed to mean?
    • "C has :function" fails where C is a class (and work properly for object of C). Any particular reasons why one cannot test the presence of a method or variable in a class without instantiating it?
    • "expr?expr:expr" construct is not mentioned in docs, but seems to work (with a very high preceedence).
    • "f().a = 0;" fails with a syntax error - i.e. "f()" is not allowed in l-value expressions. Why? "f()" could return an object, so "f().a" could be a perfectly valid l-value.
    • "a+2;" (as a statement) fails, but "(a+2);" does not. Why?


    Most - if not all - of these questions could be answered if the documentation includes a reference section with the complete grammar (in EBNF?), and a list of all built-in operations and the valid operands... Any chance of getting that?


    1. Both 'self' and 'me' both refer to 'this'. 'me' is just a holdover and might be removed in the future.
    2. Eek, will fix the documentation. I had the best of intentions to make "var x = new Dictionary()" and "var x = new Array(5)" valid, as that would make more sense than the current syntax.
    3. Static variables are essentially scoped modules. You can have a static variable/function in a class that is accessible to all members of your class. After doing the work to add them, we opted to use module functions in the API over static class functions.
    4. The isa opcode currently expects an object/module instance, but having it work with a classdef ain't a bad idea.
    5. Yup. Will add that.
    6. Will look into that.
    7. Statements usually don't allow expressions; there has to be a method invocation, an assignment, or else some other kind of statement (if/for/etc). On that note, expressions don't allow assignment (no x = y++ - --z; in Monkey C. ++/-- are just syntactic sugar for +=/-=). I think that (a+2) is falling into a special case.

    If you couldn't tell from the above answers, we're still making a few grammar tweaks. Also, since this is my first programming language, I'm scared all the compiler bullies will laugh at me once the grammar is published ("Oh, you based your BNF off of Modula 2? How *gauche*. Also, your lack of lambdas in the language gives me hives" - they're a very mean bunch.) Once it's tweaked a little more and I grow a pair, we'll include it as a programmer's guide appendix.

    -Alpha Monkey
  • Also, what are are formats and ranges for the different number types? Just like in Java or...?


    Number and Float are signed 32 bit int/float. Long/Double are signed 64 bit long/double.
  • ... but look to pages 20-22 of the programmers guide.


    I suspect you mean the table with the operations and priorities, but I still miss the possible types of the operands and results as well as the glory details like the exact semantics. E.g. which combinations of types are allowed for plus, modulo, or shift? And how do you define modulo - can it be negative?

    If we want to implement commercial applications for Connect IQ with Monkey C - and I do -, then we need these small details :-)

    (Some tests shows modulo is defined as a=(a/b)*b+(a%b) - so if a<0 then a%b<0 - where mosts mathematicians will say this is wrong :-))
  • Number and Float are signed 32 bit int/float. Long/Double are signed 64 bit long/double.


    I'm interested in the formats in Monkey C as well as the ranges of the types. The range and accuracy of float/double depends on the internal representation... But if you use an IEEE-754 32 and 64 bit floating point representations, then the ranges are given (see http://en.wikipedia.org/wiki/IEEE_floating_point).
  • 1. Both 'self' and 'me' both refer to 'this'. 'me' is just a holdover and might be removed in the future.
    2. Eek, will fix the documentation. I had the best of intentions to make "var x = new Dictionary()" and "var x = new Array(5)" valid, as that would make more sense than the current syntax.
    3. Static variables are essentially scoped modules. You can have a static variable/function in a class that is accessible to all members of your class. After doing the work to add them, we opted to use module functions in the API over static class functions.
    4. The isa opcode currently expects an object/module instance, but having it work with a classdef ain't a bad idea.
    5. Yup. Will add that.
    6. Will look into that.
    7. Statements usually don't allow expressions; there has to be a method invocation, an assignment, or else some other kind of statement (if/for/etc). On that note, expressions don't allow assignment (no x = y++ - --z; in Monkey C. ++/-- are just syntactic sugar for +=/-=). I think that (a+2) is falling into a special case.

    If you couldn't tell from the above answers, we're still making a few grammar tweaks. Also, since this is my first programming language, I'm scared all the compiler bullies will laugh at me once the grammar is published ("Oh, you based your BNF off of Modula 2? How *gauche*. Also, your lack of lambdas in the language gives me hives" - they're a very mean bunch.) Once it's tweaked a little more and I grow a pair, we'll include it as a programmer's guide appendix.

    -Alpha Monkey


    I think you (and/or Garmin) are on to something very cool and I intend to write some interesting stuff for Connect IQ. And to do that with a reasonable probability for success, requires through rough understanding of the possibilities and limitations of the platform, the language and the core libraries. Thus all the questions (and many more in the near future :-/).

    • Sorry - really bad test by me.
    • :-) I agree that you should avoid too much unnecessary sugar now if possible, and save some sugar for the future evolution of the language. The ability to write static configuration data with the array and dictionary constructs are extremely useful, but there are no use for constructors IMHO. Unnecessary sugar now can easily make future extensions of the grammar impossible :-(
    • So we should probably avoid the static keyword and just stick to using parent modules and the scoping rules...
    • Sounds good.
    • :-)
    • From my tests (basically analysing the error messages from the compiler :-/), it looks like you use l-values in several places: left side of assignments, the class for the new statement, in the enhanced (and undocumented) for-statements, in the throw-statement(?) and probably in more places. Please consider doing as in Java, C++ and many other language, where all these are proper expressions. And for the assignment, with the restriction that the "top-node" must be assignable (just as member-selection and array index is today in Monkey C). Why the restrictions in the new, for and throw statements?
    • Hmm, further tests seems to indicate that you allow all the top-precedence constructs (often named "terms" in grammars), which leads me to think that you "simply" allow term as a statement... Right?


    Please rest assured, I have absolutely no intension of bullying you or your teams mates - I still vividly remember my first language and my first compiler - and I was not successful!!! I think Monkey C can be pretty good basis for Connect IQ applications... especially if the editor and the IDE gets some needed love - more on that later.

    I would love to see the grammar now - even knowing that there will be future changes - as well as the interface of the core libraries, so I can concentrate of the writing of new apps for Connect IQ instead of second-guessing your work via a large number of tests... :-)
  • Former Member
    Former Member over 10 years ago
    I suspect you mean the table with the operations and priorities....
    In response to your question I was actually referring to the information both before & after the table. Types, bytes (thought that was what you meant by range), some declaration examples. I'm gonna go listen for the grasshopper.