Coder Social home page Coder Social logo

udop's Introduction

#udop - User defined operators A preprocessor that gives Go, JavaScript, TypeScript and Java a form of user defined operators.

It is useful for things like business logic, where you have numerous expressions involving money, which needs to be encapsulated inside a class.

##Usage: udop [-d delimchar] [-o outfile] infile

The default delimchar is @. Delimiter characters are always used in pairs. ##Format of infile The characters set support is UTF/8. Use only those characters supported by your target language.

Inside your normal Go/JavaScript/TypeScript/Java file, enclose expressions which are using your user defined operators inside a pair of pairs of delimiter characters.

var a = @@ b+c*d @@;

On output, udop will generate:

var a = b.add(c.mul(d));

Personally, I prefer to use underscores for the delimiters (easier to read), but as __ is valid in the identifiers of all the target languages, it did not seem to be a wise choice for a default.

var a = __ b+c*d __;

udop has no sense of the semantics of the underlying language. It will simply perform the straight-forward transformation of your expression. The operators and other elements all have the same relative precendence and association as in the target languages. The following operators are transformed:

Operator Method
+ .add
- .sub
* .mul
/ .div
% .mod
< .lt
<= .le
==, === .eq
!=, !== .ne
> .gt
>= .ge

Other operators present in the target languages are not included because their precedence/associativity vary between the target languages.

The short names were chosen so that the code was still somewhat readable in the output.

For your convenience, the following are passed through unchanged:

Language elements Notes
Numbers 1, 1.5, 3.7e5 May have +/-
Strings Both single and double quote forms.
Identifiers Make sure you only use characters valid in your target language.
&& The logical "and" operator
|| The logical "or" operator
! The logical "not" operator
new expr The new operator
expr[expr] Subscript
expr(arg, ...) Function call
( expr ) Parenthesis for clarity or to override associativity.
expr.id Method/property reference
? : Not available in Go

If you need to break an expression with a newline, do so immediately after an operator. The location of the newline is required due to the syntactic oddities of JavaScript, TypeScript and Go. Newline is made part of the syntax of expressions so that the line numbers in udop output matches the line numbers in udop input. This means that line numbers in error messages from the target languages are correct for both files. Obviously column numbers may not be correct in such messages.

##Escaping delimiters Because delimiters occur in pairs... escaping is a bit odd. You do not need to escape a single delimiter, but if you need two delimiters in a row, you need to type three. Some examples will help:

Example input output
"[email protected]" "[email protected]"
@ @
@@@ @@
@@@@ @@@
@@@@@@ @@@@
@@@@@@@ @@@@@
@@@@@@@@@ @@@@@@

udop's People

Contributors

briansturgill avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.