Expression evaluation syntax

This forum is for any questions about the language Tutorial D or the Rel implementation of it.
Post Reply
Chris Walton
Posts: 76
Joined: Sat Aug 18, 2012 2:13 pm

Expression evaluation syntax

Post by Chris Walton » Fri Oct 11, 2013 10:13 pm

Is there a syntax to invoke an operator and obtain its value? My intended use of this is quick and dirty syntax testing while actually writing Rel code. The example I have is:
If

Code: Select all

OPERATOR greater_than(P unbounded_integer, Q unbounded_integer) RETURNS BOOLEAN;
  CASE;
     WHEN Q = unbounded_integer("unbounded") THEN return FALSE;
     WHEN P = unbounded_integer("unbounded") AND Q <> unbounded_integer("unbounded")
        THEN return TRUE;
  ELSE return 
     (CAST_AS_INTEGER(THE_VALUE(P)) > CAST_AS_INTEGER(THE_VALUE(Q)));
  END CASE;
END OPERATOR;
is defined, I would like to write something like (in DBrowser):

greater_than(unbounded_integer("1"),unbounded_integer("1")) and get back the value of the operater, type boolean, (in this example) false.

Dave
Site Admin
Posts: 364
Joined: Sun Nov 27, 2005 7:19 pm

Re: Expression evaluation syntax

Post by Dave » Sat Oct 12, 2013 2:49 pm

If I understand what you want, type the following into DBrowser, making sure there's no ending semicolon (which makes it clear it's an expression to be evaluated rather than a statement to be executed), and press F5.

Code: Select all

greater_than(unbounded_integer("1"),unbounded_integer("1"))
Alternatively, to treat it as a statement, execute the following in DBRowser:

Code: Select all

WRITELN(greater_than(unbounded_integer("1"),unbounded_integer("1")));

Chris Walton
Posts: 76
Joined: Sat Aug 18, 2012 2:13 pm

Re: Expression evaluation syntax

Post by Chris Walton » Sat Oct 12, 2013 6:23 pm

Trying the first of your alternatives gives the error message:

ERROR: java.lang.NumberFormatException: For input string: "unbounded"

The relevant type definition is

Code: Select all

TYPE unbounded_integer
  POSSREP {VALUE CHAR CONSTRAINT 
    (CAST_AS_INTEGER(VALUE) >= 0) OR 
    (CAST_AS_INTEGER(VALUE) < 0) OR 
    VALUE = "unbounded"};
This has triggered the automatic error reporting -it came through as case 27.

Dave
Site Admin
Posts: 364
Joined: Sun Nov 27, 2005 7:19 pm

Re: Expression evaluation syntax

Post by Dave » Sat Oct 12, 2013 6:54 pm

It's doing exactly what I would expect, though I should probably change Rel to treat it as an ordinary error rather than a fatal error.

The problem is that when you try to select unbounded_integer("unbounded"), the constraint tries to perform CAST_AS_INTEGER(VALUE) with VALUE = "unbounded". CAST_AS_INTEGER("unbounded") fails with a fatal error. You probably want to use something like IS_DIGITS() from OperatorsChar.d to avoid invoking CAST_AS_INTEGER when VALUE isn't a numeric string.

Chris Walton
Posts: 76
Joined: Sat Aug 18, 2012 2:13 pm

Re: Expression evaluation syntax

Post by Chris Walton » Sun Oct 13, 2013 3:27 pm

Thank you for your suggestion. Also in the interval between asking the question and receiving your reply, I thin I can attack this in an alternative way - define unbounded_integer etc as a union data type.

Post Reply