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

struct: method #117

Open
daiyam opened this issue Feb 28, 2020 · 8 comments
Open

struct: method #117

daiyam opened this issue Feb 28, 2020 · 8 comments

Comments

@daiyam
Copy link
Member

daiyam commented Feb 28, 2020

struct Point {
    x: Number
    y: Number
	
	static distance(p1: Point, p2: Point): Number {
		
	}
}
@daiyam
Copy link
Member Author

daiyam commented Oct 1, 2023

struct will have fixed memory size.

When transpiled to JS, a struct is just an object of data. So it will have smaller memory footprint.

@daiyam
Copy link
Member Author

daiyam commented Oct 3, 2023

while objects need to have each method defined individually.

I won't do that. I will do the same as for enums: static dispatch.

enum Weekday {
    MONDAY
    TUESDAY
    WEDNESDAY
    THURSDAY
    FRIDAY
    SATURDAY
    SUNDAY

    isWeekend(): Boolean => this == SATURDAY | SUNDAY
}
func foobar(day: Weekday) {
    if day.isWeekend() {
    }
}

is transpiled as (I've removed the dispatch code when the types aren't know)

const Weekday = Helper.enum(Number, {
    MONDAY: 0,
    TUESDAY: 1,
    WEDNESDAY: 2,
    THURSDAY: 3,
    FRIDAY: 4,
    SATURDAY: 5,
    SUNDAY: 6
});
Weekday.__ks_func_isWeekend_0 = function(that) {
    return that === Weekday.SATURDAY || that === Weekday.SUNDAY;
};
function foobar(day) {
    if(Weekday.__ks_func_isWeekend_0(day)) {
    }
}

Kaoscript won't be limited to JS, it will be transpiled to other languages like Lua, Zig or Rust.

@daiyam
Copy link
Member Author

daiyam commented Oct 3, 2023

I went through the source code, I also interested in building transpilers. So it would be kind of u to suggest the resources that aided in building this project: like books, repos etc..

I'm planning a rewrite the current transpiler since the semantic analysis is limited and the code generation isn't separated with the other steps. (I already have a prototype)
So far no book, I've used articles found on internet and looked at Typescript, Nim, Zig, Babel and others compilers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
@daiyam and others