tekknolagi / stackx

MIT License
2 stars 0 forks source link

[WIP] Syntax proposal for CompilerV0 #1

Open tekknolagi opened 7 years ago

tekknolagi commented 7 years ago

It's largely based on Rust but simplified.

Variable declaration

let a : int = 1;

or for constants:

const b : string = "hello";

Reassignment

let a : int = 2;
a = "hello"; // wrong! type mismatch
a = 5; // sounds good

const b : bool = true;
b = false; // wrong! b is constant

Simple math

let a : bool = 1 + 2 == 3;

If-statements

if (condition) {
  // code...
}

or, with another branch:

if (condition) {
  // code...
} else if (another_condition) {
  // code...
} else {
  // code...
}

Switch cases

let num : int = 25;

// can also be written switch num { ...
switch (num) {
  case 25 {
    // do stuff
  }

  case 30, 40 {
    // do stuff if num is 30 or 40
  }

  case (25, 30, 35) {
    // can also use parens
  }

  default {

  }
}

Loops

While

while (true) {
  // do a thing
}

For

for (let x : int = 0; x < 10; x += 1) {
 // do a thing
}

Functions

func add_two_numbers (a : int, b : int) : int {
  return a + b;
}

Structs

struct Slice {
  buffer : byte*,
  size : int
};
akkartik commented 7 years ago

Tell me what our default language is so that we can minimize such issues :)

tekknolagi commented 7 years ago

@akkartik default language? Do you mean the sort of basis parent language whose syntax we are simplifying?

akkartik commented 7 years ago

Yes, it shall be Rust. :)

tekknolagi commented 7 years ago

I've decided to revert to C-style while and for loops because we do not have iterators or anything fancy that Rust-style "requires".

KCreate commented 7 years ago

Still not hooked on having := as our assignment operator :/

tekknolagi commented 7 years ago

How do you feel about set a = 5;?

tekknolagi commented 7 years ago

Or we could go the route of normal a = 5.

tekknolagi commented 7 years ago

Thoughts on let/const vs let/let mut?

KCreate commented 7 years ago

if (set a = read()) { ... } ??

tekknolagi commented 7 years ago

@KCreate are you suggesting that it should be required in loops to use the word set?

KCreate commented 7 years ago

No, I think requiring set at all is a bad idea

I'd say we'll go for the normal way (a = 5).

Regarding let/const vs let/let mut, I'd prefer let/const. let mut makes mut look like some kind of attribute.

akkartik commented 7 years ago

Does Rust have :=?

tekknolagi commented 7 years ago

No; https://doc.rust-lang.org/book/variable-bindings.html

akkartik commented 7 years ago

Well then, let's follow Rust :)

akkartik commented 7 years ago

I notice your conditional and loop examples above use parens:

if (condition) {
  // code...
} else if (another_condition) {
  // code...
} else {
  // code...
}

Let's just follow Rust exactly:

if condition {
  // code...
} else if another_condition {
  // code...
} else {
  // code...
}
KCreate commented 7 years ago

@akkartik Charly has this too :)

KCreate commented 7 years ago

Just added syntax for a switch statement

tekknolagi commented 7 years ago

How easy do we want this first compiler to be? I don't know how we're weighing that against number of features / ease of implementing another language in this compiled one, but I think that should be discussed at some level of technical detail.

KCreate commented 7 years ago

I'd go for C level simplicity, maybe even less.

KCreate commented 7 years ago

I've added syntax highlighting using the swift tag.

Also some other points:

tekknolagi commented 7 years ago

I'd say no to the second one but don't know enough / can't think enough at the moment to offer an opinion on namespacing.

KCreate commented 7 years ago

What should be the syntax to create pointer types?

I'd suggest type*

tekknolagi commented 7 years ago

Only if we make it left-associative. I have severe issues with people who write C-style pointers like that:

int* a, b; // misleading! only a is a pointer
KCreate commented 7 years ago

What about making it right-associative?

*int that is.

Are there languages who have a different pointer syntax?

tekknolagi commented 7 years ago

Then it's kind of annoying for dereferencing. I think keeping it C/Rust like in form makes sense, but changing the associativity would be a strong preference.

KCreate commented 7 years ago

Why would that interfere with dereferencing?

BTW, what's rust's pointer syntax?

tekknolagi commented 7 years ago

Since traditionally dereferencing has used that exact syntax. Rust is (very nearly) the same as C.

KCreate commented 7 years ago

Our syntax is context free so I don't think we'll have to worry about that.

How about switching the meaning of * and &?

let foo : *int; // pointer to int
*test; // pointer to variable test
&test; // dereference test
tekknolagi commented 7 years ago

Hard veto on that. Very confusing. We don't have to worry about it at a technical level, sure, but as someone who TAs students quite regularly... they already get confused with the dual meaning of * in C/C++. This would not help.

KCreate commented 7 years ago

I see. I however don't see a problem with using the *int syntax.

We know exactly in our syntax when the type grammar has to kick in, so it can't be confused with the dereference operator.

let a : *int = ...;
        ^
        |
        +- The parser knows it has to parse this expression as a type.

tmp = 5 *int;
        ^
        |
        +- This is "5 times int", where int would be a semantic error as it's a reserved name.
tekknolagi commented 7 years ago

Yeah that's fine, I think; I merged that intro master.