Page 1 of 2

Operator overloading support?

PostPosted: Thu Nov 24, 2011 11:35 am
by melger
I just recently discovered Cobra, and I really like the syntax/speed trade-off. One question I have is if operator overloading support is planned anytime soon? I realize that full support would be a large undertaking, but just the ability to consume C# classes with operators defined (i.e. issue #69 from 2008) would be very useful for people doing numerical-oriented applications.

Re: Operator overloading support?

PostPosted: Thu Nov 24, 2011 11:42 am
by Charles
It wasn't already high on my list as I usually get by using the various methods like:
start = DateTime.now
...
duration = DateTime.now.subtract(start)

But I realize there are libraries out there that violate the CLS/CTS standards and do not provide named methods for their operators which can be a bummer. I'll take a closer look in December. After all, I'm heading out for some turkey today. :D

Btw if you get in a jam, you can use the sharp "escape hatch":
start = DateTime.now
...
duration = sharp'DateTime.Now - start' to TimeSpan

Re: Operator overloading support?

PostPosted: Thu Nov 24, 2011 12:01 pm
by melger
Thanks for the quick reply! That looks interesting... Is the sharp escape hatch just quoted C# code?

Re: Operator overloading support?

PostPosted: Fri Nov 25, 2011 2:29 am
by Charles
Re: the "sharp string", yes. Only works for expressions and statements, not declarations such as methods and classes.

Re: Operator overloading support?

PostPosted: Fri Jan 25, 2013 12:26 pm
by DelphiGuy
charles, just curious: has there been any further activity on providing operator overload support? thanks.

Re: Operator overloading support?

PostPosted: Fri Jan 25, 2013 5:25 pm
by Charles
No. I have been focused on bug fixing and supporting the IDE effort (specifically optimizing Cobra.Compiler.dll).

Re: Operator overloading support?

PostPosted: Tue Jan 29, 2013 8:10 am
by torial
What kinds of changes have you been making for IDE support? I noticed a lot of optimization changes. Are you adding hooks into the compilation mechanism?

Re: Operator overloading support?

PostPosted: Wed Jan 30, 2013 10:20 am
by nerdzero
Charles did a lot of work on the performance of the Tokenizer which means less time waiting between entering some text in MonoDevelop and seeing the completion data.

Re: Operator overloading support?

PostPosted: Wed Jan 30, 2013 10:38 am
by torial
How are you getting access to the tokenizer info? I took a quick look at the code in GitHub but couldn't figure out where autocomplete logic was being handled.

Re: Operator overloading support?

PostPosted: Wed Jan 30, 2013 12:48 pm
by nerdzero
Yeah, the code isn't very easy to understand at this point. I'm working on making it prettier. Also, if you were looking at the 'master' branch there is no autocomplete logic in there, it's in the 'completion' branch.

I'm not using the tokenizer output directly, but I am using the output of the parser which is fed by the tokenizer. Here's a simplified example program:

use Cobra.Compiler

class AstDemo

def main

# set some default options for the compiler
options = OptionValues()
options.add("compile", true)
options.add("back-end", "clr")
options.add("turbo", true)
options.add("number", decimal)

# create an instance of the Cobra Compiler
verbosity = 0
compiler = Compiler(verbosity)
compiler.options = options
compiler.initBackEnd

# set some shared variables on the Node class
Node.setCompiler(compiler)
Node.typeProvider = compiler

# create an instance of the Cobra Parser
parser = CobraParser()
parser.typeProvider = compiler
parser.warningRecorder = compiler
parser.errorRecorder = compiler
parser.globalNS = compiler.globalNS
parser.backEnd = compiler.backEnd

# create a Cobra source "file", you could also read this from the disk
fileName = "example.cobra"

sourceCode = _
"class A\n" +
" var b as int\n" +
" var c as int\n" +
" def add as int\n" +
" return .b + .c"

# parse the source code and get a prelimary AST
module as CobraModule? = nil

try
module = parser.parseSource(fileName, sourceCode)
compiler.modules.add(module)
catch ex as SourceException
print "Parser error: [ex.toString]"

if module <> nil
# print the nodes in the abstract syntax tree
for syntaxNode in module.topNameSpace.declsInOrder
if syntaxNode inherits Box
print syntaxNode.name
for member in syntaxNode.declsInOrder
print "\t[member.name]"


Output:

Code: Select all
A
   b
   c
   add