Comments (2)
Code from the elm compiler that special cases some infix operators
The variable name
in each code snippet is the name of an elm function. Sometimes these functions are exposed via infix operators (e.g. infix left 6 (+) = add
) and sometimes elm function calls are turned into operators by the elm compiler (e.g. the elm code complement 67
becomes ~67
in js).
Operators in Basics
Source: https://github.com/elm/compiler/blob/master/compiler/src/Generate/JavaScript/Expression.hs#L503-L546
generateBasicsCall :: Mode.Mode -> ModuleName.Canonical -> Name.Name -> [Opt.Expr] -> JS.Expr
generateBasicsCall mode home name args =
case args of
[elmArg] ->
let arg = generateJsExpr mode elmArg in
case name of
"not" -> JS.Prefix JS.PrefixNot arg
"negate" -> JS.Prefix JS.PrefixNegate arg
"toFloat" -> arg
"truncate" -> JS.Infix JS.OpBitwiseOr arg (JS.Int 0)
_ -> generateGlobalCall home name [arg]
[elmLeft, elmRight] ->
case name of
-- NOTE: removed "composeL" and "composeR" because of this issue:
-- https://github.com/elm/compiler/issues/1722
"append" -> append mode elmLeft elmRight
"apL" -> generateJsExpr mode $ apply elmLeft elmRight
"apR" -> generateJsExpr mode $ apply elmRight elmLeft
_ ->
let
left = generateJsExpr mode elmLeft
right = generateJsExpr mode elmRight
in
case name of
"add" -> JS.Infix JS.OpAdd left right
"sub" -> JS.Infix JS.OpSub left right
"mul" -> JS.Infix JS.OpMul left right
"fdiv" -> JS.Infix JS.OpDiv left right
"idiv" -> JS.Infix JS.OpBitwiseOr (JS.Infix JS.OpDiv left right) (JS.Int 0)
"eq" -> equal left right
"neq" -> notEqual left right
"lt" -> cmp JS.OpLt JS.OpLt 0 left right
"gt" -> cmp JS.OpGt JS.OpGt 0 left right
"le" -> cmp JS.OpLe JS.OpLt 1 left right
"ge" -> cmp JS.OpGe JS.OpGt (-1) left right
"or" -> JS.Infix JS.OpOr left right
"and" -> JS.Infix JS.OpAnd left right
"xor" -> JS.Infix JS.OpNe left right
"remainderBy" -> JS.Infix JS.OpMod right left
_ -> generateGlobalCall home name [left, right]
_ ->
generateGlobalCall home name (map (generateJsExpr mode) args)
Functions in Bitwise
Source: https://github.com/elm/compiler/blob/master/compiler/src/Generate/JavaScript/Expression.hs#L481-L500
generateBitwiseCall :: ModuleName.Canonical -> Name.Name -> [JS.Expr] -> JS.Expr
generateBitwiseCall home name args =
case args of
[arg] ->
case name of
"complement" -> JS.Prefix JS.PrefixComplement arg
_ -> generateGlobalCall home name args
[left,right] ->
case name of
"and" -> JS.Infix JS.OpBitwiseAnd left right
"or" -> JS.Infix JS.OpBitwiseOr left right
"xor" -> JS.Infix JS.OpBitwiseXor left right
"shiftLeftBy" -> JS.Infix JS.OpLShift right left
"shiftRightBy" -> JS.Infix JS.OpSpRShift right left
"shiftRightZfBy" -> JS.Infix JS.OpZfRShift right left
_ -> generateGlobalCall home name args
_ ->
generateGlobalCall home name args
Operators in Tuple
Source: https://github.com/elm/compiler/blob/master/compiler/src/Generate/JavaScript/Expression.hs#L460-L470
generateTupleCall :: ModuleName.Canonical -> Name.Name -> [JS.Expr] -> JS.Expr
generateTupleCall home name args =
case args of
[value] ->
case name of
"first" -> JS.Access value (JsName.fromLocal "a")
"second" -> JS.Access value (JsName.fromLocal "b")
_ -> generateGlobalCall home name args
_ ->
generateGlobalCall home name args
Operators in JsArray
Source: https://github.com/elm/compiler/blob/master/compiler/src/Generate/JavaScript/Expression.hs#L473-L478
generateJsArrayCall :: ModuleName.Canonical -> Name.Name -> [JS.Expr] -> JS.Expr
generateJsArrayCall home name args =
case args of
[entry] | name == "singleton" -> JS.Array [entry]
[index, array] | name == "unsafeGet" -> JS.Index array index
_ -> generateGlobalCall home name args
from compiler.
So the special casing of operators is more complicated. Sometimes (a vast majority of real world cases) the elm compiler special cases these operators. Othertimes it falls back to calling the kernel implementation of the arithmetic operation.
Here is an image (here is it running live - but link will expire when I switch my laptop running the server off). On the left is elm code and on the right is the relevant parts of the compiled javascript.
The elm compiler cannot special case the use of +
in addSlow
and so it falls back to the function in the Basics
module.
As a tangent this is dangerous as a bug in elm/core
could cause the result of addSlow
to diverge from add
- would be a very nasty bug to track down and would only occur vary rarely.
from compiler.
Related Issues (20)
- Left field idea: rename types to `T` and drop exposing HOT 3
- Fresh nix install yields elm version mismatch HOT 4
- Parse error on record HOT 2
- [Security] Workflow deploy-to-gh-pages.yml is using vulnerable action JamesIves/github-pages-deploy-action HOT 2
- Jakt backend
- Document what elm/core kernel functions can be done in pure Elm HOT 12
- Custom types: parser tests HOT 5
- Document the "elm type" of kernel functions HOT 2
- Inlining optimizations
- Make records not support multiple fields of the same name
- Introduce benchmarks and tracking of performance over time
- meta-elm use cases HOT 1
- Use "parameter", not "argument", in Lambda type HOT 1
- Add tests for things like `f a b c < 5` HOT 1
- Make the let body see the let bindings HOT 5
- question about Slack bots HOT 3
- Wrong type inference for function type HOT 3
- Path handling in elm-in-elm HOT 1
- Make sure dependencies in all elm.jsons are the same HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from compiler.