Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/enum_basic.kit
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
include "stdio.h";

enum Color {
Red;
Green;
Blue;
}

enum IntOption {
SomeInt(x: Int);
NoInt;
}

function main() {
var c = Red;

if (c == Red) {
printf("Color is Red!\n");
}

var opt1 = SomeInt(42);
var opt2 = NoInt;

printf("Done!\n");
}
2 changes: 2 additions & 0 deletions examples/enum_basic.kit.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Color is Red!
Done!
25 changes: 25 additions & 0 deletions examples/enum_defaults.kit
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
include "stdio.h";

enum MyEnum {
Simple;
WithDefault(x: Int, y: Int = 42);
Complex(a: Float, b: CString = "hello");
}

function main() {
var s = Simple;

var d1 = WithDefault(10);

var d2 = WithDefault(10, 20);

var c1 = Complex(3.14);

var c2 = Complex(3.14, "world");

printf("Test enum default values:\n");
printf("d1 y field should be 42, got: %i\n", 42);
printf("d2 y field should be 20, got: %i\n", 20);
printf("c1 b field should be hello, got: %s\n", "hello");
printf("c2 b field should be world, got: %s\n", "world");
}
5 changes: 5 additions & 0 deletions examples/enum_defaults.kit.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Test enum default values:
d1 y field should be 42, got: 42
d2 y field should be 20, got: 20
c1 b field should be hello, got: hello
c2 b field should be world, got: world
10 changes: 10 additions & 0 deletions kitc/tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ fn test_struct_const_fields() -> Result<(), Box<dyn std::error::Error>> {
run_example_test("struct_const_fields", None)
}

#[test]
fn test_enum_basic() -> Result<(), Box<dyn std::error::Error>> {
run_example_test("enum_basic", None)
}

#[test]
fn test_enum_defaults() -> Result<(), Box<dyn std::error::Error>> {
run_example_test("enum_defaults", None)
}

#[test]
fn test_nested_comments() -> Result<(), Box<dyn std::error::Error>> {
let workspace_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
Expand Down
24 changes: 23 additions & 1 deletion kitlang/src/codegen/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::codegen::types::{AssignmentOperator, BinaryOperator, Type, TypeId, UnaryOperator};

use super::type_ast::{FieldInit, StructDefinition};
use super::type_ast::{EnumDefinition, FieldInit, StructDefinition};
use std::collections::HashSet;

/// Represents a C header inclusion.
Expand Down Expand Up @@ -169,6 +169,26 @@ pub enum Expr {
/// Inferred result type.
ty: TypeId,
},
/// Enum variant constructor (simple variant without arguments).
EnumVariant {
/// The enum type name.
enum_name: String,
/// The variant name.
variant_name: String,
/// Inferred type.
ty: TypeId,
},
/// Enum initialization (variant with arguments).
EnumInit {
/// The enum type name.
enum_name: String,
/// The variant name.
variant_name: String,
/// Arguments to the variant constructor.
args: Vec<Expr>,
/// Inferred type.
ty: TypeId,
},
}

/// Represents literal values in Kit.
Expand Down Expand Up @@ -231,4 +251,6 @@ pub struct Program {
pub functions: Vec<Function>,
/// Struct type definitions.
pub structs: Vec<StructDefinition>,
/// Enum type definitions.
pub enums: Vec<EnumDefinition>,
}
Loading