Skip to content

Commit

Permalink
Remove object from type declaration.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Jan 13, 2024
1 parent 803546b commit 843471f
Show file tree
Hide file tree
Showing 59 changed files with 136 additions and 142 deletions.
41 changes: 20 additions & 21 deletions docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ There are `25` general keywords. This list categorizes them:
These keywords only have meaning in a certain context.
- [Methods](#methods): `self`, `Self`
- [Catching Errors](#caught-variable): `caught`
- [Object Type](#objects): `object`
- [Enum Type](#enums): `enum`
- [Function Throws](#throws-specifier): `throws`

Expand Down Expand Up @@ -767,14 +766,14 @@ var colors = []:
```

## Objects.
Any value that isn't a primitive is an object. You can declare your own object types using the `type object` declaration.
Any value that isn't a primitive is an object. You can declare your own object types using the `type` declaration.
Object types are similar to structs and classes in other languages.
Unlike classes, there is no concept of inheritance at the language level.

### Fields.
Fields must be declared at the top of the `type object` block using `var` or `my`:
Fields must be declared at the top of the `type` block using `var` or `my`:
```cy
type Node object:
type Node:
var value int
var next any
```
Expand All @@ -790,7 +789,7 @@ print node.value -- Prints "123"
### Methods.
Methods allow invoking a function on an object instance using the `.` operator:
```cy
type Node object:
type Node:
var value int
var next any

Expand All @@ -816,7 +815,7 @@ Type members can be implicitly referenced inside the method. *Incomplete: Only t

To reference members explicitly inside a method, use the builtin `self`:
```cy
type Node object:
type Node:
var value int
var next any

Expand All @@ -827,7 +826,7 @@ type Node object:
### Type functions.
Type functions are declared outside of the `type` block with an explicit namespace path:
```cy
type Node object:
type Node:
var value int
var next any

Expand Down Expand Up @@ -1410,7 +1409,7 @@ func foo(): -- Exported static function.

var .bar = 234 -- Exported static variable.

type Thing object: -- Exported type.
type Thing: -- Exported type.
var a float
```

Expand Down Expand Up @@ -1593,7 +1592,7 @@ When using `cfunc` or `cbind` declarations, [symbols](#symbols-1) are used to re
| .double | float | double |
| (1) .charPtr | pointer | char* |
| .voidPtr | pointer | void* |
| (2) type {S} object | type {S} object | struct |
| (2) type {S} | type {S} | struct |
1. Use `os.cstr()` and `pointer.fromCstr()` to convert between a Cyber string and a null terminated C string.
2. The mapping from a Cyber object type `S` and the C-struct can be declared with `cbind`.
Expand All @@ -1603,7 +1602,7 @@ When using `cfunc` or `cbind` declarations, [symbols](#symbols-1) are used to re
```cy
import os

type MyObject object:
type MyObject:
var a float
var b pointer
var c bool
Expand Down Expand Up @@ -2190,9 +2189,9 @@ var .global Map = [:]
Unlike local variables, static variable declarations do not infer the type from the right hand side. A specific type must be specified or it will default to the `any` type.

### Object types.
A `type object` declaration creates a new object type. Field types are optional and declared with a type specifier after their name.
A `type` declaration creates a new object type. Field types are optional and declared with a type specifier after their name.
```cy
type Student object: -- Creates a new type named `Student`
type Student: -- Creates a new type named `Student`
var name string
var age int
var gpa float
Expand All @@ -2209,15 +2208,15 @@ print s.gpa -- Prints "0.0"
Circular type dependencies are allowed if the object can be initialized:
> _Planned Feature: Optional types are not currently supported._
```cy
type Node object:
type Node:
var val any
var next Node? -- Valid type specifier.
```
In this example, `next` has an optional `Node?` type so it can be initialized to `none` when creating a new `Node` object.

The following example will fail because this version of `Node` can not be initialized:
```cy
type Node object:
type Node:
var val any
var next Node

Expand All @@ -2237,8 +2236,8 @@ The following shows the zero values of builtin or created types.
|`array`|`array('')`|
|`List`|`[]`|
|`Map`|`[:]`|
|`type S object`|`[S:]`|
|`@host type S object`|`S.$zero()`|
|`type S`|`[S:]`|
|`@host type S`|`S.$zero()`|
|`dynamic`|`none`|
|`any`|`none`|
|`S?`|`none`|
Expand Down Expand Up @@ -2351,7 +2350,7 @@ Normally this would impact performance, but Cyber generates specialized bytecode

To overload an operator for an object type, declare `$prefix`, `$infix`, `$postfix` methods. See the available [builtin operators](#builtin-operators). Since operator names aren't allowed as standard identifiers, they are contained in a string literal.
```cy
type Vec2 object:
type Vec2:
var x float
var y float

Expand All @@ -2371,7 +2370,7 @@ var c = -a

Some special operators have their own name. This example overloads the `index` operator and the `set index` operator:
```cy
type MyCollection object:
type MyCollection:
var arr List

func '$index'(idx):
Expand Down Expand Up @@ -2419,7 +2418,7 @@ A list of all supported operators:
Declare a `$call` function to allow invoking a module as a function.
```cy
-- Object types are also modules.
type Vec2 object:
type Vec2:
var x float
var y float

Expand All @@ -2436,7 +2435,7 @@ var v = Vec2(1, 2)
Declare a `$missing` method as a fallback when a method was not found in an instance.
> _Planned Feature_
```cy
type A object:
type A:

func '$missing'(args...):
return args.len
Expand Down Expand Up @@ -2571,7 +2570,7 @@ bool modLoader(CsVM* vm, CsStr spec, CsModuleLoaderResult* out) {
"@host var .MyList List\n"
"\n"
"@host\n"
"type MyCollection object:\n"
"type MyCollection:\n"
" @host func asList() any"
"\n"
"@host func MyCollection.new(a, b) MyCollection\n";
Expand Down
4 changes: 2 additions & 2 deletions docs/gen-docs.cy
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ type State enum:
case header
case html

type Link object:
type Link:
var href string
var title string
var text string
Expand Down Expand Up @@ -374,7 +374,7 @@ func getAttrText(attr) string:
return ''
return (attr.text as pointer).toArray(0, attr.size).decode()

type ModulePair object:
type ModulePair:
var path string
var section string

Expand Down
2 changes: 1 addition & 1 deletion examples/account.cy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Account object:
type Account:
var name string
var balance float

Expand Down
2 changes: 1 addition & 1 deletion examples/c-embedded/bind_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ bool modLoader(CsVM* vm, CsStr spec, CsModuleLoaderResult* out) {
"@host var .MyList List\n"
"\n"
"@host\n"
"type MyCollection object:\n"
"type MyCollection:\n"
" @host func asList() any"
"\n"
"@host func MyCollection.new(a, b) MyCollection\n";
Expand Down
2 changes: 1 addition & 1 deletion examples/web-playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
for str[cur..].findRune(0u'c') as i:
print 'Found char at {cur + i}.'
cur += i + 1`,
objects: `type Node object:
objects: `type Node:
data any
next Node
func new(data):
Expand Down
2 changes: 1 addition & 1 deletion exts/sublime/cyber.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contexts:
- match: '\b(coyield|coresume|coinit)\b'
scope: keyword.coroutine.cyber

- match: '\b(type|enum|object|trait|true|false|none)\b'
- match: '\b(type|enum|trait|true|false|none)\b'
scope: keyword.type.cyber

- match: '\b(try|catch|error|throw)\b'
Expand Down
32 changes: 16 additions & 16 deletions src/builtins/builtins.cy
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@
@host func typesym(val any) symbol

@host
type bool object
type bool

--| Converts a value to either `true` or `false`.
@host func bool.'$call'(val any) bool

@host
type error object:
type error:
--| Return the underlying `symbol`.
@host func sym() symbol

--| Create an error from an enum or symbol.
@host func error.'$call'(val any) error

@host
type int object:
type int:
@host func '$prefix~'() int
@host func '$prefix-'() int
@host func '$infix<'(o any) bool
Expand Down Expand Up @@ -96,7 +96,7 @@ type int object:
@host func int.'$call'(val any) int

@host
type float object:
type float:
@host func '$prefix-'() float
@host func '$infix<'(o any) bool
@host func '$infix<='(o any) bool
Expand All @@ -113,7 +113,7 @@ type float object:
@host func float.'$call'(val any) float

@host
type List object:
type List:
@host func '$index'(idx any) any
@host func '$setIndex'(idx any, val any) none
@host func '$slice'(start any, end any) List
Expand Down Expand Up @@ -164,15 +164,15 @@ type List object:
@host func List.fill(val any, n int) List

@host
type ListIterator object:
type ListIterator:
@host func next() any

@host
type tuple object:
type tuple:
@host func '$index'(idx int) any

@host
type Map object:
type Map:
@host func '$index'(key any) any
@host func '$setIndex'(key any, val any) none

Expand All @@ -186,11 +186,11 @@ type Map object:
@host func iterator() MapIterator

@host
type MapIterator object:
type MapIterator:
@host func next() any

@host
type string object:
type string:
@host func '$infix+'(o any) string

--| Returns a new string that concats this string and `str`.
Expand Down Expand Up @@ -258,7 +258,7 @@ type string object:
@host func string.'$call'(val any) string

@host
type array object:
type array:
@host func '$infix+'(o any) array

--| Returns a new array that concats this array and `other`.
Expand Down Expand Up @@ -335,7 +335,7 @@ type array object:
--| Converts a string to an byte `array`.
@host func array.'$call'(val any) array

type arrayIterator object:
type arrayIterator:
var arr array
var nextIdx int

Expand All @@ -347,7 +347,7 @@ type arrayIterator object:
return res

@host
type pointer object:
type pointer:
--| Returns the memory address as an `int`. The value may be negative since it's
--| bitcasted from an unsigned 48-bit integer but it retains the original pointer bits.
@host func addr() int
Expand All @@ -371,15 +371,15 @@ type pointer object:
@host func pointer.'$call'(val any) pointer

@host
type ExternFunc object:
type ExternFunc:
--| Returns the memory address as an `int`. The value may be negative since it's
--| bitcasted from an unsigned 48-bit integer but it retains the original pointer bits.
@host func addr() int

@host
type Fiber object:
type Fiber:
@host func status() symbol

@host
type metatype object:
type metatype:
@host func id() int
2 changes: 1 addition & 1 deletion src/jit/gen-stencils-a64.cy
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if codeBuf == none:

var llSymIter = llvm.ObjectFileCopySymbolIterator(llBin)

type Sym object:
type Sym:
var name string
var addr int

Expand Down
2 changes: 1 addition & 1 deletion src/jit/gen-stencils-x64.cy
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if codeBuf == none:

var llSymIter = llvm.ObjectFileCopySymbolIterator(llBin)

type Sym object:
type Sym:
var name string
var addr int
var code array
Expand Down
Loading

0 comments on commit 843471f

Please sign in to comment.