deathbeam / spoon

:ramen: Spoon is a programming language that runs blazingly fast, compiles to native code and works everywhere.
https://spoonlang.org
MIT License
56 stars 11 forks source link

import syntax suggestion #26

Open escargotprodige opened 8 years ago

escargotprodige commented 8 years ago

So, I've been thinking about a new syntax for import statements for a while, and I think I found something that most people will like. I'm opening this issue so that people can discuss about this syntax in a peaceful way, in order to make the perfect syntax (well, maybe not everybody will like it, but I hope that most people will).
Keep in mind that this syntax is still a WIP and I think has a lot of room for improvements.

The syntax was inspired by these languages:

It's also really flexible so it fits multiple cases & personal preferences.

Alright, so the syntax would follow these simple rules:

The same applies to subpackages:

from luxe import [
    Input, Scene, Sprite
    Visual, Color, AppConfig
    Particles, Entity, Text
] #CoffeeScript arrays are used for list of modules
from luxe.structural import [Pool, Bag]
from luxe.options import GeometryOptions

from mint import Control
from mint.types import Types
from mint.render.luxe import [LuxeMintRender, Convert]
from mint.layout.margins import Margins

import states

output:

import luxe.Input;
import luxe.Scene;
import luxe.Sprite;
import luxe.Visual;
import luxe.Color;
import luxe.Vector;
import luxe.AppConfig;
import luxe.Particles;
import luxe.Entity;
import luxe.Text;
import luxe.structural.Pool;
import luxe.structural.Bag;
import luxe.options.GeometryOptions;

import mint.Control;
import mint.types.Types;
import mint.render.luxe.LuxeMintRender;
import mint.render.luxe.Convert;
import mint.layout.margins.Margins;

import states.*;

Import blocks

As you can see, the example code is quite redundant (lots of from & import), so it could also support an optional syntax for less redundancy in the code:

import! #the exclamation mark tells the compiler that this is an import block
  luxe:
    <<: [
      Input, Scene, Sprite
      Visual, Color, AppConfig
      Particles, Entity, Text
    ] #the << child is used for modules that are part of the parent package (here luxe)
    structural: [Pool, Bag] #child packages are nested objects
    options: GeometryOptions
  mint:
    <<: Control
    types: Types
    render.luxe: [LuxeMintRender, Convert]
    layout.margins: Margins #when there is no need to nest a child, use the . shorthand
  states: *

This code would be compiled in the same way as the previous example. It would work the exact same way as CoffeeScript object notation, except for the first item in the list (as you can see in the example above)

Conditional compilation


So, what do you think about the syntax? Please share your opinions below as it is the only way we can progress :smile:

Take a look at the gist

P.S. Sorry for long issue, here's some potatoes potatoes

deathbeam commented 8 years ago

This looks really awesome, and I will definitely try to implement it. Not sure about that "Import blocks" tho.