Giter Site home page Giter Site logo

Ternary operators about umka-lang HOT 4 OPEN

skejeton avatar skejeton commented on May 24, 2024 1
Ternary operators

from umka-lang.

Comments (4)

skejeton avatar skejeton commented on May 24, 2024

The syntax is up to choice here, we could do something like this, gdscript inspired:

fn max(a, b: int) {
  return b if a < b else a
}

(notice the check expression is now in the middle)

from umka-lang.

vtereshkov avatar vtereshkov commented on May 24, 2024

I like this proposal. As for the syntax, I think that b if a < b else a is very hard to compile for a single-pass compiler that generates bytecode on-the-fly. The compiler sees b but does not know whether to insert a jump instruction before it to skip this b if a < b yields false.

from umka-lang.

marekmaskarinec avatar marekmaskarinec commented on May 24, 2024

I would like ternary operators in umka. There are a lot of cases where they would be useful. As for the syntax proposed by @vtereshkov, I think it would imply that switch statement s and maybe even loops support this syntax too. Something to think about?

from umka-lang.

vtereshkov avatar vtereshkov commented on May 24, 2024

There is an unexpected issue with reference counting that we need to solve before we can implement the ternary operator. Consider the following code:

        for i := 0; i < 2; i++ {
                // ...
                p = i % 2 == 0 ? new(int, 42) : new(int, 43)
                // ...
        }

Since every reference-counted function result is stored in a temporary local variable to prevent its premature death, the actually executed pseudocode is like this:

        for i := 0; i < 2; i++ {
                // ...
                p = i % 2 == 0 ? {__temp0 = new(int, 42); __temp0} : {__temp1 = new(int, 43); __temp1}
                // ...
                decref(__temp0)
                decref(__temp1)
        }

This means that, e.g., __temp0 will get some reference-counted value only once (at iteration 0), but have its reference count decremented twice (at iterations 0 and 1). It may cause a dangling pointer.

Notice that we cannot call decref(__temp0) immediately in the innermost {} block, since __temp0 must at least outlive the assignment to p.

from umka-lang.

Related Issues (20)

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.