Zen-C
Loading...
Searching...
No Matches
zprep.h File Reference
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Include dependency graph for zprep.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Token
 Represents a source token. More...
 
struct  Lexer
 Lexer state. More...
 
struct  CompilerConfig
 Compiler configuration and flags. More...
 

Macros

#define z_is_windows()   0
 
#define ZEN_VERSION   "0.1.0"
 Zen-C version.
 
#define COLOR_RESET   "\033[0m"
 Reset color.
 
#define COLOR_RED   "\033[1;31m"
 Red color.
 
#define COLOR_GREEN   "\033[1;32m"
 Green color.
 
#define COLOR_YELLOW   "\033[1;33m"
 Yellow color.
 
#define COLOR_BLUE   "\033[1;34m"
 Blue color.
 
#define COLOR_CYAN   "\033[1;36m"
 Cyan color.
 
#define COLOR_BOLD   "\033[1m"
 Bold text.
 
#define free(ptr)   ((void)0)
 Free memory.
 
#define malloc(sz)   xmalloc(sz)
 Allocate memory.
 
#define realloc(p, s)   xrealloc(p, s)
 Reallocate memory.
 
#define calloc(n, s)   xcalloc(n, s)
 Allocate and zero memory.
 
#define MAX_FLAGS_SIZE   1024
 
#define MAX_PATH_SIZE   1024
 
#define MAX_PATTERN_SIZE   1024
 

Enumerations

enum  TokenType {
  TOK_EOF = 0 , TOK_IDENT , TOK_INT , TOK_FLOAT ,
  TOK_STRING , TOK_FSTRING , TOK_CHAR , TOK_LPAREN ,
  TOK_RPAREN , TOK_LBRACE , TOK_RBRACE , TOK_LBRACKET ,
  TOK_RBRACKET , TOK_LANGLE , TOK_RANGLE , TOK_COMMA ,
  TOK_COLON , TOK_SEMICOLON , TOK_OP , TOK_AT ,
  TOK_DOTDOT , TOK_DOTDOT_EQ , TOK_DOTDOT_LT , TOK_ARROW ,
  TOK_PIPE , TOK_TEST , TOK_ASSERT , TOK_SIZEOF ,
  TOK_DEF , TOK_DEFER , TOK_AUTOFREE , TOK_QUESTION ,
  TOK_USE , TOK_QQ , TOK_QQ_EQ , TOK_Q_DOT ,
  TOK_DCOLON , TOK_TRAIT , TOK_IMPL , TOK_AND ,
  TOK_OR , TOK_FOR , TOK_COMPTIME , TOK_ELLIPSIS ,
  TOK_UNION , TOK_ASM , TOK_VOLATILE , TOK_ASYNC ,
  TOK_AWAIT , TOK_PREPROC , TOK_ALIAS , TOK_COMMENT ,
  TOK_OPAQUE , TOK_UNKNOWN
}
 Token types for the Lexer. More...
 

Functions

void lexer_init (Lexer *l, const char *src)
 Initialize the lexer.
 
Token lexer_next (Lexer *l)
 Get the next token.
 
Token lexer_peek (Lexer *l)
 Get the next token without advancing.
 
Token lexer_peek2 (Lexer *l)
 Get the next token without advancing (2 look ahead).
 
void register_trait (const char *name)
 Register a trait.
 
int is_trait (const char *name)
 Check if a name is a trait.
 
void * xmalloc (size_t size)
 Allocate memory.
 
void * xrealloc (void *ptr, size_t new_size)
 Reallocate memory.
 
void * xcalloc (size_t n, size_t size)
 Allocate and zero memory.
 
char * xstrdup (const char *s)
 Duplicate a string.
 
void zpanic (const char *fmt,...)
 Error reporting.
 
void zpanic_at (Token t, const char *fmt,...)
 Error reporting with token location.
 
char * load_file (const char *filename)
 Load a file.
 
void scan_build_directives (struct ParserContext *ctx, const char *src)
 Scan build directives.
 
int levenshtein (const char *s1, const char *s2)
 Calculate Levenshtein distance.
 
void zpanic_with_suggestion (Token t, const char *msg, const char *suggestion)
 Error reporting with suggestion.
 
void error_undefined_function (Token t, const char *func_name, const char *suggestion)
 Error reporting for undefined function.
 
void error_wrong_arg_count (Token t, const char *func_name, int expected, int got)
 Error reporting for wrong argument count.
 
void error_undefined_field (Token t, const char *struct_name, const char *field_name, const char *suggestion)
 Error reporting for undefined field.
 
void error_type_expected (Token t, const char *expected, const char *got)
 Error reporting for type expected.
 
void error_cannot_index (Token t, const char *type_name)
 Error reporting for cannot index.
 
void zwarn (const char *fmt,...)
 Warning reporting.
 
void zwarn_at (Token t, const char *fmt,...)
 Warning reporting with token location.
 
void warn_unused_variable (Token t, const char *var_name)
 Warning reporting for unused variable.
 
void warn_unused_parameter (Token t, const char *param_name, const char *func_name)
 Warning reporting for unused parameter.
 
void warn_shadowing (Token t, const char *var_name)
 Warning reporting for shadowing.
 
void warn_unreachable_code (Token t)
 Warning reporting for unreachable code.
 
void warn_implicit_conversion (Token t, const char *from_type, const char *to_type)
 Warning reporting for implicit conversion.
 
void warn_narrowing_conversion (Token t, const char *from_type, const char *to_type)
 Warning reporting for narrowing conversion.
 
void warn_missing_return (Token t, const char *func_name)
 Warning reporting for missing return.
 
void warn_comparison_always_true (Token t, const char *reason)
 Warning reporting for comparison always true.
 
void warn_comparison_always_false (Token t, const char *reason)
 Warning reporting for comparison always false.
 
void warn_division_by_zero (Token t)
 Warning reporting for division by zero.
 
void warn_integer_overflow (Token t, const char *type_name, long long value)
 Warning reporting for integer overflow.
 
void warn_array_bounds (Token t, int index, int size)
 Warning reporting for array bounds.
 
void warn_format_string (Token t, int arg_num, const char *expected, const char *got)
 Warning reporting for format string.
 
void warn_null_pointer (Token t, const char *expr)
 Warning reporting for null pointer.
 

Variables

char * g_current_filename
 Current filename.
 
char g_link_flags [MAX_FLAGS_SIZE]
 
char g_cflags [MAX_FLAGS_SIZE]
 
int g_warning_count
 
CompilerConfig g_config
 

Macro Definition Documentation

◆ calloc

#define calloc (   n,
 
)    xcalloc(n, s)

Allocate and zero memory.

◆ COLOR_BLUE

#define COLOR_BLUE   "\033[1;34m"

Blue color.

◆ COLOR_BOLD

#define COLOR_BOLD   "\033[1m"

Bold text.

◆ COLOR_CYAN

#define COLOR_CYAN   "\033[1;36m"

Cyan color.

◆ COLOR_GREEN

#define COLOR_GREEN   "\033[1;32m"

Green color.

◆ COLOR_RED

#define COLOR_RED   "\033[1;31m"

Red color.

◆ COLOR_RESET

#define COLOR_RESET   "\033[0m"

Reset color.

◆ COLOR_YELLOW

#define COLOR_YELLOW   "\033[1;33m"

Yellow color.

◆ free

#define free (   ptr)    ((void)0)

Free memory.

◆ malloc

#define malloc (   sz)    xmalloc(sz)

Allocate memory.

◆ MAX_FLAGS_SIZE

#define MAX_FLAGS_SIZE   1024

◆ MAX_PATH_SIZE

#define MAX_PATH_SIZE   1024

◆ MAX_PATTERN_SIZE

#define MAX_PATTERN_SIZE   1024

◆ realloc

#define realloc (   p,
 
)    xrealloc(p, s)

Reallocate memory.

◆ z_is_windows

#define z_is_windows ( )    0

◆ ZEN_VERSION

#define ZEN_VERSION   "0.1.0"

Zen-C version.

Enumeration Type Documentation

◆ TokenType

enum TokenType

Token types for the Lexer.

Enumerator
TOK_EOF 

End of File.

TOK_IDENT 

Identifier (variable, function name).

TOK_INT 

Integer literal.

TOK_FLOAT 

Float literal.

TOK_STRING 

String literal.

TOK_FSTRING 

Formatted string literal (f"val is {x}").

TOK_CHAR 

Character literal.

TOK_LPAREN 

(

TOK_RPAREN 

)

TOK_LBRACE 

{

TOK_RBRACE 

}

TOK_LBRACKET 

[

TOK_RBRACKET 

]

TOK_LANGLE 

<

TOK_RANGLE 

TOK_COMMA 

,

TOK_COLON 

:

TOK_SEMICOLON 

;

TOK_OP 

General operator (e.g. +, *, /).

TOK_AT 

@

TOK_DOTDOT 

..

TOK_DOTDOT_EQ 

..= (inclusive range).

TOK_DOTDOT_LT 

..< (exclusive range, explicit).

TOK_ARROW 

-> or =>

TOK_PIPE 

|> (pipe operator).

TOK_TEST 

'test' keyword.

TOK_ASSERT 

'assert' keyword.

TOK_SIZEOF 

'sizeof' keyword.

TOK_DEF 

'def' keyword.

TOK_DEFER 

'defer' keyword.

TOK_AUTOFREE 

'autofree' keyword.

TOK_QUESTION 

?

TOK_USE 

'use' keyword.

TOK_QQ 

?? (null coalescing).

TOK_QQ_EQ 

??=

TOK_Q_DOT 

?. (optional chaining).

TOK_DCOLON 

::

TOK_TRAIT 

'trait' keyword.

TOK_IMPL 

'impl' keyword.

TOK_AND 

'and' keyword.

TOK_OR 

'or' keyword.

TOK_FOR 

'for' keyword.

TOK_COMPTIME 

'comptime' keyword.

TOK_ELLIPSIS 

...

TOK_UNION 

'union' keyword.

TOK_ASM 

'asm' keyword.

TOK_VOLATILE 

'volatile' keyword.

TOK_ASYNC 

'async' keyword.

TOK_AWAIT 

'await' keyword.

TOK_PREPROC 

Preprocessor directive (#...).

TOK_ALIAS 

'alias' keyword.

TOK_COMMENT 

Comment (usually skipped).

TOK_OPAQUE 

'opaque' keyword.

TOK_UNKNOWN 

Unknown token.

Function Documentation

◆ error_cannot_index()

void error_cannot_index ( Token  t,
const char *  type_name 
)

Error reporting for cannot index.

Here is the call graph for this function:

◆ error_type_expected()

void error_type_expected ( Token  t,
const char *  expected,
const char *  got 
)

Error reporting for type expected.

Here is the call graph for this function:

◆ error_undefined_field()

void error_undefined_field ( Token  t,
const char *  struct_name,
const char *  field_name,
const char *  suggestion 
)

Error reporting for undefined field.

Here is the call graph for this function:

◆ error_undefined_function()

void error_undefined_function ( Token  t,
const char *  func_name,
const char *  suggestion 
)

Error reporting for undefined function.

Here is the call graph for this function:

◆ error_wrong_arg_count()

void error_wrong_arg_count ( Token  t,
const char *  func_name,
int  expected,
int  got 
)

Error reporting for wrong argument count.

Here is the call graph for this function:

◆ is_trait()

int is_trait ( const char *  name)

Check if a name is a trait.

Here is the caller graph for this function:

◆ levenshtein()

int levenshtein ( const char *  s1,
const char *  s2 
)

Calculate Levenshtein distance.

Here is the caller graph for this function:

◆ lexer_init()

void lexer_init ( Lexer l,
const char *  src 
)

Initialize the lexer.

Here is the caller graph for this function:

◆ lexer_next()

Token lexer_next ( Lexer l)

Get the next token.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lexer_peek()

Token lexer_peek ( Lexer l)

Get the next token without advancing.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lexer_peek2()

Token lexer_peek2 ( Lexer l)

Get the next token without advancing (2 look ahead).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_file()

char * load_file ( const char *  filename)

Load a file.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ register_trait()

void register_trait ( const char *  name)

Register a trait.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scan_build_directives()

void scan_build_directives ( struct ParserContext ctx,
const char *  src 
)

Scan build directives.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_array_bounds()

void warn_array_bounds ( Token  t,
int  index,
int  size 
)

Warning reporting for array bounds.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_comparison_always_false()

void warn_comparison_always_false ( Token  t,
const char *  reason 
)

Warning reporting for comparison always false.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_comparison_always_true()

void warn_comparison_always_true ( Token  t,
const char *  reason 
)

Warning reporting for comparison always true.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_division_by_zero()

void warn_division_by_zero ( Token  t)

Warning reporting for division by zero.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_format_string()

void warn_format_string ( Token  t,
int  arg_num,
const char *  expected,
const char *  got 
)

Warning reporting for format string.

Here is the call graph for this function:

◆ warn_implicit_conversion()

void warn_implicit_conversion ( Token  t,
const char *  from_type,
const char *  to_type 
)

Warning reporting for implicit conversion.

Here is the call graph for this function:

◆ warn_integer_overflow()

void warn_integer_overflow ( Token  t,
const char *  type_name,
long long  value 
)

Warning reporting for integer overflow.

Here is the call graph for this function:

◆ warn_missing_return()

void warn_missing_return ( Token  t,
const char *  func_name 
)

Warning reporting for missing return.

Here is the call graph for this function:

◆ warn_narrowing_conversion()

void warn_narrowing_conversion ( Token  t,
const char *  from_type,
const char *  to_type 
)

Warning reporting for narrowing conversion.

Here is the call graph for this function:

◆ warn_null_pointer()

void warn_null_pointer ( Token  t,
const char *  expr 
)

Warning reporting for null pointer.

Here is the call graph for this function:

◆ warn_shadowing()

void warn_shadowing ( Token  t,
const char *  var_name 
)

Warning reporting for shadowing.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_unreachable_code()

void warn_unreachable_code ( Token  t)

Warning reporting for unreachable code.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_unused_parameter()

void warn_unused_parameter ( Token  t,
const char *  param_name,
const char *  func_name 
)

Warning reporting for unused parameter.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ warn_unused_variable()

void warn_unused_variable ( Token  t,
const char *  var_name 
)

Warning reporting for unused variable.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xcalloc()

void * xcalloc ( size_t  n,
size_t  size 
)

Allocate and zero memory.

Here is the caller graph for this function:

◆ xmalloc()

void * xmalloc ( size_t  size)

Allocate memory.

◆ xrealloc()

void * xrealloc ( void *  ptr,
size_t  new_size 
)

Reallocate memory.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ xstrdup()

char * xstrdup ( const char *  s)

Duplicate a string.

Here is the call graph for this function:

◆ zpanic()

void zpanic ( const char *  fmt,
  ... 
)

Error reporting.

◆ zpanic_at()

void zpanic_at ( Token  t,
const char *  fmt,
  ... 
)

Error reporting with token location.

Here is the caller graph for this function:

◆ zpanic_with_suggestion()

void zpanic_with_suggestion ( Token  t,
const char *  msg,
const char *  suggestion 
)

Error reporting with suggestion.

Here is the caller graph for this function:

◆ zwarn()

void zwarn ( const char *  fmt,
  ... 
)

Warning reporting.

Here is the caller graph for this function:

◆ zwarn_at()

void zwarn_at ( Token  t,
const char *  fmt,
  ... 
)

Warning reporting with token location.

Here is the caller graph for this function:

Variable Documentation

◆ g_cflags

char g_cflags[]
extern

◆ g_config

CompilerConfig g_config
extern

◆ g_current_filename

char* g_current_filename
extern

Current filename.

◆ g_link_flags

char g_link_flags[]
extern

◆ g_warning_count

int g_warning_count
extern