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

langdefs: use case expression for function bodies #113

Merged
merged 2 commits into from
Feb 1, 2025

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Jan 30, 2025

Summary

Instead of a list of assignments of the form <f> <pattern>* = <body>,
the body of a meta-function must now be a case expression.

Details

A function such as:

function f, x -> x:
  f(z) = 1
  f(1) = 2

is now written as:

function f, x -> x:
  case _
  of z: 1
  of 1: 2 

The two reasons for this change are:

  1. there's no point in repeatedly specifying the name of a function:
    it's the only valid identifier that may appear in before the
    parenthesis. It's presence only introduces visual noise and makes
    it harder to rename functions

  2. the assignment based syntax is a bit trickier to parse, and it's
    arguably not very intuitive. For people already familiar with
    NimSkull and its syntax, what the new case expression syntax does
    is likely easier to understand

Using a case expression also makes else available to be used for
representing the fallthrough case (the new parser already supports
this).

Two things that might be slightly confusing are that:

  1. contrary to the NimSkull semantics, the of branches in the
    DSL are ordered
  2. in the DSL, the body of the of branches also affects whether the
    branch is actually "picked"

The macro's parsing logic is updated to handle the new syntax, and the
source language specification is changed to make use of it.


Notes For Reviewers

Personally, I think the new syntax is better and more intuitive (despite the differences in semantics compared to NimSkull), but I'm also okay with keeping the previous syntax (and thus not merging this PR).

@zerbina zerbina added the enhancement New feature or request label Jan 30, 2025
@zerbina zerbina requested a review from saem January 30, 2025 23:16
Copy link
Contributor

@saem saem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I thought we could have bare of branches, but then I realized that's probably a grammar issue, I think the case _ is a small "price" to pay for something that writes about as elegantly as elixir or ocaml.

@saem saem merged commit 1d2e064 into nim-works:main Feb 1, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants