haskell - Type checking of infix operators in compiler -


i'm writing compiler (in haskell) , in grammar of language there rules add infix operators (addition used example):

eadd . expr ::= expr "+" expr 

which means eadd expression, it's consist of expression, string "+" , expression.

parser returns abstract syntax tree (ast):

data expr = ... | eadd expr expr 

i want make typechecker if checks calls of functions given arguments of correct types.

note, "+" function takes 2 integers , returns integer. other operators similar.

at moment came 3 approaches typechecking eadd, of them include adding "+" function initial symbol table:

  1. declare infix plus syntax sugar calling function "+" 2 arguments. put "desugarizer" converts ast parser another data type (without eadd) in between parser , typechecker.

  2. (similar first) declare infix plus syntax sugar, desugarizer uses same ast data type. typechecker returns error when it's given eadd.

  3. inline "desugarizer" typechecker. similar this:

    ... typecheck (eadd b) = typecheck (ecall infixplus [a, b]) ... 

note, binary infix operators subject (other arithmetic, boolean operations, comparison operators).

it seems first approach correct one. means later in compiler pipeline, particularly in code generator, ecalls should handled special cases, because in compilers output (in case — llvm) these functions supposed inlined (unlike usual function calls). means codegen has list of functions calls handled differently other function calls.

what best approach issue?

upd

how similar issue handled in haskell (from https://ghc.haskell.org/trac/ghc/wiki/commentary/compiler/renamer):

... renamer following things:

  • sort out fixities. parser parses infix applications left-associative, regardless of fixity. example "a + b * c" parsed "(a + b) * c". renamer re-associates such nested operator applications, using fixities declared in module.

llvm supports inline attributes e.g.

define void @f() alwaysinline { ... } 

so 1 option treat + normal function call , let llvm optimization job.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -