开发者

Implementing operator overloading and inline functions

i am building a small scale interpreter. so far i have the capability to calculate mathematical expressions including variables and several functions (such as MOD, MAX etc...). i need to add two capabilities:

Online Operator Ove开发者_JAVA百科rloading Operator overloading is a term which refers to the creation of new operators which have the functionality of one of your existing functions

so for example after typing overload *# POW, 3 *# 2 will result 9.

Inline Function Creation In addition to operator overloading, you will also need to support inline function creation4. In other words, you will support the creation of new functions, using the following syntax: inline _FUNC{@arg1, @arg2, ...}

So, i have no idea where to start. If anyone can link me somewhere i can read about known techniques or existing java tools that might help me, that would be very helpfull. I don't even know what to look for. thanks!


you need to handle the new keywords while performing lexical analysis in your lexer.

for example, if the user enters overload *# POW, your lexer should be able to identify 3 tokens: "overload", "*#" and "POW". similarly, for the function creation, your lexer needs to tokenize the curly braces (amount of whitespace/line-breaks between curly braces is insignificant).

next, you need to modify your parser and build a syntax tree. for example, after performing syntax analysis on 3 *# 2, your syntax tree should be able to identify what are the operators and what are the operands.

the final step is to run through the syntax tree and interpret/evaluate the results.

as for tools, there are lexer analyzers and parser generators. if you already have some existing code for your lexer and parser, i suggest that you avoid these tools and just make the necessary modifications to the existing code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜