Skip to content

PoorlyDefinedBehaviour/toy-language-csharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Toy language

Usage

dotnet run Program.cs (Repl mode)
dotnet run Program.cs --debug (Debug mode)
dotnet run Program cs file.toy (Execute file mode)
fn dumpUser(user) {
  print user.name + " " + user.surname + ", " + user.age
}

class User {}

const user = User()
user.name = "john"
user.surname = "doe"
user.age = 20

dumpUser(user)

Usage

clone this repo
run `yarn`
modify main.yajs
yarn `yarn start`

Syntax

Output

print "hello world!"

Comments

// This is a single line comment

/* 
  this 
  is
  a
  block
  comment
*/

Values

"hello world" // string
1 // number
null // null
true // boolean
false // boolean

Variables

let a
...
a = "some value"

let b = 10
let c = "value"
let d = 20
let e = b + d

const PI = 3.14159265359

const cantChangeThisValue = "hello world!"
cantChangeThisValue = "hello world!!!!!" // error

const myVariable // error, const variable must be initialized

Null and Null coalescing

const a = null ?? "hello world" // hello world

...

fn getValue() {
  return "value"
}

const a = null ?? getValue() // value

...

fn getValue() {
  return 10
}

const b = getValue() ?? "other value" // 10

...

fn getValue() {
  return null
}

const c = getValue() ?? "other value" // other value

Operators

const a = 10
const b = 20

const c = a + b
const d = a * b
const e = a / b
const f = a mod b

const aLessThanB = a < b
const aBiggerThanB = a > b

const aEqualsB = a == b
const aDifferentThanB = a != b

const f = !true

const f1 = true and false
const t = true or false

print !!10             // true
print !!0              // false
print !!"hello world!" // true
print !!""             // false
print !!null           // false

Scope

const i = 10
print i // 10

{
  const i = 20
  print i // 20
}

print i // 10

Control flow

if(true) {

}

...

if(true and false) {

} else {

}

...

if(true or false) {

} else {

}

if(!false) {

}

...

let a = 10
if(a < 20) {

}

...

let x = "hello"
if(x == "hello world!"){

} else {

}

For loop

for(let i = 0; i < 10; i = i + 1) {

}

...

let j = 0;
for( ; j < 5; j = j + 1) {

}

...

for( ; ; ) {

}

While loop

while(true) {
  ...
}

...

let i = 0
while(i < 10) {
  i = i + 1
}

fns

fn sayHello(name){
  print "Hello " + name
}

let name = "world"
sayHello(name)

...

fn fibonacci(n) {
  if(n < 2) {
    return n
  }
  return fibonacci(n - 1) + fibonacci(n - 2)
}

print fibonacci(5)

...

fn factorial(n) {
  if(n < 2) {
    return n
  }
  return n * factorial(n - 1)
}

print factorial(5)

Closures

fn makeCounter() {
  let i = 0

  fn count() {
    i = i + 1
    return i
  }

  return count
}

const count = makeCounter()
print count()
print count()

Classes

class Foo {}
const fooInstance = Foo()

...

class Foo {
  sayHello(name) {
    print "hello " + name
  }
}

const fooInstance = Foo()
fooInstance.sayHello("world!")

...

fn dumpUser(user) {
  print user.name + " " + user.surname + ", " + user.age
}

class User {}

const user = User()
user.name = "john"
user.surname = "doe"
user.age = 20

dumpUser(user)

...

class User {
  dump() {
    print this.name + ", " + this.age
  }
}

const user = User()
user.name = "john"
user.age = 20

user.dump()

...

class User {
  constructor(name, age){
    this.name = name
    this.age = age
  }

  static test() {
    print "static works"
  }

  dump() {
    print this.name + ", " + this.age
  }
}

User.test()

const user = User("john", 20)
user.dump()

user.test() // Error: Object <<User>> has no property called <test>

...

class Entity {
  sayHello(name) {
    print "entity says hello to " + name
  }
}

class User extends Entity {

}

const user = User()
user.sayHello("world!")

...

class Entity {
  sayHello(name) {
    print "entity says hello to " + name
  }
}

class User extends Entity {
 sayHello(name){
   print "user says hello to " + name
   super.sayHello(name)
 }
}

const user = User()
user.sayHello("world!")

About

toy interpreted programming language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages