vn/code/vn_scene.h

423 lines
10 KiB
C
Raw Normal View History

/* date = July 30th 2023 7:45 pm */
2023-07-19 15:09:41 +00:00
#ifndef VN_SCENE_H
#define VN_SCENE_H
// sixten(IMPORTANT): The arena that the scene is compiled on
// is assumed to be the same it runs on, i.e. the interpreter relies
// on the memory addresses being the same during compilation and
// runtime.
// We also assume that the original text and tokenization remains in
// memory, for the duration of the scene.
2023-07-19 15:09:41 +00:00
////////////////////////////////
//~ sixten: Scene Compilation Types
2023-08-22 03:19:51 +00:00
struct scene_compile_error
{
scene_compile_error *Next;
string Message;
token Token;
};
struct scene_compile_error_list
{
scene_compile_error *First;
scene_compile_error *Last;
s64 Count;
};
enum scene_opcode
2023-07-19 15:09:41 +00:00
{
S_Op_Invalid = 0,
S_Op_Constant,
2023-09-05 17:50:49 +00:00
S_Op_Pop,
S_Op_Nil,
S_Op_True,
S_Op_False,
S_Op_Add,
S_Op_Subtract,
S_Op_Multiply,
S_Op_Divide,
S_Op_Equal,
S_Op_Greater,
S_Op_Less,
S_Op_Negate,
S_Op_Not,
2023-07-19 15:09:41 +00:00
S_Op_DefineGlobal,
S_Op_GetGlobal,
S_Op_SetGlobal,
2023-07-19 15:09:41 +00:00
2023-08-22 03:19:51 +00:00
S_Op_Jump,
S_Op_JumpClose,
S_Op_AddBranch,
S_Op_Halt,
S_Op_AwaitInput,
2023-10-04 17:21:15 +00:00
S_Op_ClearDialog,
S_Op_LineEntry,
2023-09-13 04:42:11 +00:00
S_Op_ShowCharacter,
2023-07-19 15:09:41 +00:00
};
struct scene_bytecode_chunk
2023-07-19 15:09:41 +00:00
{
scene_bytecode_chunk *Next;
s64 Count;
u8 Data[4096];
2023-07-19 15:09:41 +00:00
};
struct scene_annotated_bytecode_chunk
2023-07-19 15:09:41 +00:00
{
scene_annotated_bytecode_chunk *Next;
string Name;
s64 Count;
u8 Data[4096];
2023-07-19 15:09:41 +00:00
};
struct scene_annotated_bytecode_bucket
2023-07-19 15:09:41 +00:00
{
scene_annotated_bytecode_chunk *First;
scene_annotated_bytecode_chunk *Last;
2023-07-19 15:09:41 +00:00
s64 Count;
};
enum scene_value_kind
{
2023-09-05 17:50:49 +00:00
S_ValueKind_Nil = 0,
S_ValueKind_Number,
S_ValueKind_Boolean,
S_ValueKind_Pointer,
2023-08-22 03:19:51 +00:00
S_ValueKind_SourceRef,
2023-09-05 17:50:49 +00:00
S_ValueKind_String,
2023-08-22 03:19:51 +00:00
S_ValueKind_Offset,
};
struct scene_value
{
scene_value_kind Kind;
union
{
r64 Number;
b32 Boolean;
u64 Pointer;
2023-08-22 03:19:51 +00:00
range1_s64 SourceRef;
2023-10-04 17:21:15 +00:00
string String;
2023-08-22 03:19:51 +00:00
s64 Offset;
};
};
struct scene_value_chunk
{
scene_value_chunk *Next;
s64 Count;
scene_value Values[512];
};
enum scene_precedence
{
S_Precedence_None,
S_Precedence_Assignment,
S_Precedence_Or,
S_Precedence_And,
S_Precedence_Equality,
S_Precedence_Comparison,
S_Precedence_Term,
S_Precedence_Factor,
S_Precedence_Unary,
S_Precedence_Call,
S_Precedence_Primary,
};
typedef void scene_parse_function(struct scene_compiler *Compiler, b32 CanAssign);
struct scene_parse_rule
2023-07-19 15:09:41 +00:00
{
scene_parse_function *PrefixRule;
scene_parse_function *InfixRule;
scene_precedence Precedence;
2023-07-19 15:09:41 +00:00
};
2023-08-22 03:19:51 +00:00
enum scene_emission_target_type
{
S_EmissionTarget_Raw,
S_EmissionTarget_Named,
};
struct scene_emission_target
{
memory_arena *Arena;
scene_annotated_bytecode_bucket *Bucket;
scene_emission_target_type Type;
string Name;
};
struct scene_branch_case
{
scene_branch_case *Next;
token Name;
scene_annotated_bytecode_bucket Bucket;
scene_value *OffsetValue;
scene_value *EndOffsetValue;
};
2023-10-04 17:21:15 +00:00
struct scene_character_action
2023-09-05 17:50:49 +00:00
{
2023-10-04 17:21:15 +00:00
scene_character_action *Next;
scene_character_action *Prev;
string Target;
character_state State;
2023-09-05 17:50:49 +00:00
};
struct scene_compiler
2023-07-19 15:09:41 +00:00
{
memory_arena *Arena;
2023-08-22 03:19:51 +00:00
b32 InPanicMode;
b32 EncounteredError;
scene_compile_error_list Errors;
string Text;
token *TokensBegin;
2023-07-19 15:09:41 +00:00
token *TokensEnd;
token *At;
scene_annotated_bytecode_bucket GlobalScope;
scene_annotated_bytecode_bucket ProcBuckets[32];
2023-08-22 03:19:51 +00:00
scene_emission_target TargetStack[16];
s32 TargetStackIndex;
scene_value_chunk *FirstValueChunk;
scene_value_chunk *LastValueChunk;
s64 ValueCount;
2023-07-19 15:09:41 +00:00
};
////////////////////////////////
//~ sixten: Compiled Scene Types
struct scene_proc
2023-07-19 15:09:41 +00:00
{
// sixten: scene data
string Name;
u8 *Data;
s64 Count;
// sixten: hash link
scene_proc *Next;
2023-07-19 15:09:41 +00:00
};
struct scene_proc_bucket
{
scene_proc *First;
scene_proc *Last;
};
2023-07-19 15:09:41 +00:00
struct compiled_scene
{
scene_proc_bucket Buckets[16];
scene_value *Values;
s64 ValueCount;
string Source;
2023-08-22 03:19:51 +00:00
scene_compile_error_list Errors;
b32 IsValid;
};
////////////////////////////////
//~ sixten: Scene Runtime Types
struct scene_runtime_error
{
scene_runtime_error *Next;
scene_runtime_error *Prev;
string Message;
};
struct scene_runtime_result
{
b32 HadError;
b32 ReachedAwait;
};
enum textbox_action_kind
{
TextboxActionKind_Set,
TextboxActionKind_Append,
};
struct textbox_action
{
textbox_action *Next;
textbox_action_kind Kind;
string String;
};
struct branch_case
{
string Name;
s64 Offset;
};
2023-09-05 17:50:49 +00:00
struct scene_runtime_stack
{
scene_value Stack[128];
s32 Count;
};
2023-08-22 03:19:51 +00:00
struct scene_runtime
{
compiled_scene Compiled;
// sixten: runtime state
2023-10-04 17:21:15 +00:00
memory_arena *RuntimeArena;
2023-08-22 03:19:51 +00:00
scene_proc *CurrentProc;
s64 IP;
2023-09-05 17:50:49 +00:00
scene_runtime_stack Stack;
2023-08-22 03:19:51 +00:00
// sixten: errors
memory_arena *ErrorArena;
scene_runtime_error *FirstError;
scene_runtime_error *LastError;
// sixten: branches
branch_case Branches[16];
s64 BranchCount;
// sixten: result
textbox_action *FirstTextboxAction;
textbox_action *LastTextboxAction;
2023-10-04 17:21:15 +00:00
scene_character_action *FirstCharacterAction;
scene_character_action *LastCharacterAction;
2023-08-22 03:19:51 +00:00
branch_case *FirstBranchCase;
scene_runtime_result LastResult;
};
2023-07-19 15:09:41 +00:00
////////////////////////////////
//~ sixten: Scene Compiler Functions
2023-08-22 03:19:51 +00:00
//- sixten: value helpers
2023-09-05 17:50:49 +00:00
inline scene_value S_MakeNil(void)
{
scene_value Result;
Result.Kind = S_ValueKind_Nil;
return(Result);
}
inline scene_value S_MakeNumber(r64 Value)
{
scene_value Result;
Result.Kind = S_ValueKind_Number;
Result.Number = Value;
return(Result);
}
inline scene_value S_MakeBoolean(b32 Value)
{
scene_value Result;
Result.Kind = S_ValueKind_Boolean;
Result.Boolean = Value;
return(Result);
}
inline scene_value S_MakePointer(void *Value)
{
scene_value Result;
Result.Kind = S_ValueKind_Pointer;
Result.Pointer = PointerToU64(Value);
return(Result);
}
2023-08-22 03:19:51 +00:00
inline scene_value S_MakeSourceRef(token Token)
{
scene_value Result;
Result.Kind = S_ValueKind_SourceRef;
Result.SourceRef = Token.Range;
return(Result);
}
inline scene_value S_MakeOffset(s64 Offset)
{
scene_value Result;
Result.Kind = S_ValueKind_Offset;
Result.Offset = Offset;
return(Result);
}
//- sixten: error messaging
2023-08-22 03:19:51 +00:00
static void S_ParseError(scene_compiler *Compiler, char *Message, s64 TokenOffset = -1);
//- sixten: bytecode helpers
static void S_EmitByte(scene_compiler *Compiler, u8 Byte);
2023-09-05 17:50:49 +00:00
static void S_EmitU32(scene_compiler *Compiler, u32 Value);
2023-08-22 03:19:51 +00:00
static void S_EmitBucket(scene_compiler *Compiler, scene_annotated_bytecode_bucket *Bucket);
static u64 S_MakeConstant(scene_compiler *Compiler, scene_value Value);
static void S_EmitConstant(scene_compiler *Compiler, scene_value Value);
2023-08-22 03:19:51 +00:00
static void S_SetEmissionTarget(scene_compiler *Compiler, scene_emission_target Target);
static void S_PushEmissionTarget(scene_compiler *Compiler, scene_emission_target Target);
static void S_PopEmissionTarget(scene_compiler *Compiler);
static scene_annotated_bytecode_chunk *S_FindBytecodeChunkByName(scene_compiler *Compiler, string Name);
2023-08-22 03:19:51 +00:00
inline scene_emission_target S_RawEmissionTarget(memory_arena *Arena, scene_annotated_bytecode_bucket *Bucket)
{
scene_emission_target Target = {};
Target.Arena = Arena;
Target.Bucket = Bucket;
Target.Type = S_EmissionTarget_Raw;
return(Target);
}
inline scene_emission_target S_NamedEmissionTarget(memory_arena *Arena, scene_annotated_bytecode_bucket *Bucket, string Name)
{
scene_emission_target Target = {};
Target.Arena = Arena;
Target.Bucket = Bucket;
Target.Type = S_EmissionTarget_Named;
Target.Name = Name;
return(Target);
}
//- sixten: parsing helpers
static void S_AdvanceCompiler(scene_compiler *Compiler);
static scene_parse_rule S_ParseRuleFromToken(scene_compiler *Compiler, token Token);
static b32 S_MatchToken(scene_compiler *Compiler, token Token, token_kind Kind);
static token S_ConsumeToken(scene_compiler *Compiler, token_kind Kind, char *Message);
//- sixten: parsing
static void S_ParseTopLevelDeclaration(scene_compiler *Compiler);
static void S_ParseProcedure(scene_compiler *Compiler);
static void S_ParseDeclaration(scene_compiler *Compiler);
static void S_ParseVariableDeclaration(scene_compiler *Compiler);
static void S_ParseVariable(scene_compiler *Compiler, b32 CanAssign);
2023-08-22 03:19:51 +00:00
static void S_ParseNamedVariable(scene_compiler *Compiler, token Token, b32 CanAssign);
static void S_ParseLineEntry(scene_compiler *Compiler);
2023-08-22 03:19:51 +00:00
static void S_ParseJumpStatement(scene_compiler *Compiler);
static void S_ParseBranchStatement(scene_compiler *Compiler);
static scene_branch_case *S_ParseBranchCase(scene_compiler *Compiler, memory_arena *Arena);
static void S_ParseStatement(scene_compiler *Compiler);
static void S_ParseExpression(scene_compiler *Compiler);
static void S_ParseLiteral(scene_compiler *Compiler, b32 CanAssign);
static void S_ParseNumber(scene_compiler *Compiler, b32 CanAssign);
2023-09-05 17:50:49 +00:00
static void S_ParseString(scene_compiler *Compiler, b32 CanAssign);
static void S_ParseGrouping(scene_compiler *Compiler, b32 CanAssign);
static void S_ParseUnary(scene_compiler *Compiler, b32 CanAssign);
static void S_ParseBinary(scene_compiler *Compiler, b32 CanAssign);
static void S_ParsePrecedence(scene_compiler *Compiler, scene_precedence Precedence);
//- sixten: debugging
static string S_DisassembleBytecode(scene_compiler *Compiler, scene_annotated_bytecode_chunk *Chunk, memory_arena *Arena);
2023-08-22 03:19:51 +00:00
//- sixten: compilation
static compiled_scene S_ScriptFromText(memory_arena *Arena, string Text);
2023-08-22 03:19:51 +00:00
static compiled_scene S_CopyCompiledScene(memory_arena *Arena, compiled_scene *Compiled);
2023-08-22 03:19:51 +00:00
////////////////////////////////
//~ sixten: Scene Runtime Functions
static scene_proc *S_FindProcByName(compiled_scene *Compiled, string Name);
2023-10-04 17:21:15 +00:00
static void S_ResetRuntime(scene_runtime *Runtime);
2023-08-22 03:19:51 +00:00
static scene_runtime_result S_Run(scene_runtime *Runtime, memory_arena *FrameArena, b32 AdvanceOnAwait);
2023-07-19 15:09:41 +00:00
#endif //VN_SCENE_H