Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement let expression #40

Open
mauriciotogneri opened this issue Aug 28, 2024 · 0 comments
Open

Implement let expression #40

mauriciotogneri opened this issue Aug 28, 2024 · 0 comments
Assignees
Labels
feature New feature or request language Language related issues

Comments

@mauriciotogneri
Copy link
Collaborator

mauriciotogneri commented Aug 28, 2024

Let node

  • assignments: [AssignmentNode]
  • node: Node

AssignmentNode

  • variable: String
  • node: Node

Approach

  • Constraint: none of the variables in the expression can be called like any of the parameters of the function
  • When substituting in a let expression
    • Substitute all bindings in the expression of the first assignment
    • Add the first variable (with its node substituted) to the bindings
    • Use the new bindings to substitute in the second assignment
    • Add the second variable (with its node substituted) to the bindings
    • Continue for all variables
    • Use the final substitution for the let expression node
foo(n) = let
             x = bar(n)
         in
             calculate(x, x + 1)
foo(n) = let
             x = bar(n)
         in
             let
                 y = test(n)
             in
                 calculate(x, x + 1, y, y + 1)
foo(n) = let
             x = bar(n)
             y = test(n)
         in
             calculate(x, x + 1, y, y + 1)
foo(a, b) = let
                x = a + b
                y = x * 2
            in
                num.pow(y, 2)
foo(a, b) = let
                x = process(a, b)
            in
                if (result.isSuccess(x))
                    console.write('Success: ' + result.get(x))
                else
                    console.write('Failure: ' + result.get(x))
frequency(list) = frequency.helper(list, {})

frequency.helper(list, result) =
    if (list.isEmpty(list))
        result
    else
        let
            first = list.first(list)
            tail = list.tail(list)
        in
            let count = if (map.containsKey(result, first))
                            result[first] + 1
                        else
                            1
            in
                frequency.helper(tail, map.set(result, first, count))

main = frequency([1, 2, 2, 3, 1, 4, 5, 2, 2, 5])
@mauriciotogneri mauriciotogneri added feature New feature or request language Language related issues labels Aug 28, 2024
@mauriciotogneri mauriciotogneri added this to the 0.5.0 - New language features milestone Aug 28, 2024
@mauriciotogneri mauriciotogneri self-assigned this Aug 28, 2024
@mauriciotogneri mauriciotogneri moved this to Todo in sdk Aug 28, 2024
@mauriciotogneri mauriciotogneri modified the milestones: 0.4.0 - Copper Chisel, 0.5.0 - Bronze Axe Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request language Language related issues
Projects
Status: Todo
Development

No branches or pull requests

1 participant