Skip to content

Commit

Permalink
add basic @doc functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Dec 9, 2024
1 parent 62d25bc commit 0704a72
Show file tree
Hide file tree
Showing 12 changed files with 414 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/core/chuck.lex
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ global { adjust(); return GLOBAL; }
"@construct" { adjust(); return AT_CTOR; }
"@destruct" { adjust(); return AT_DTOR; }
"@import" { adjust(); return AT_IMPORT; }
"@doc" { adjust(); return AT_DOC; }
"->" { adjust(); return ARROW_RIGHT; }
"<-" { adjust(); return ARROW_LEFT; }
"-->" { adjust(); return GRUCK_RIGHT; }
Expand Down
43 changes: 34 additions & 9 deletions src/core/chuck.y
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ a_Program g_program = NULL;
a_Complex complex_exp;
a_Polar polar_exp;
a_Vec vec_exp; // ge: added 1.3.5.3
a_Import import; // 1.5.2.5 (ge) added
a_Import import; // 1.5.4.0 (ge) added
a_Doc doc; // 1.5.4.4 (ge) added
};

// expect shift/reduce conflicts
Expand All @@ -108,8 +109,9 @@ a_Program g_program = NULL;
// 1.4.0.0: changed to 41 for global keyword
// 1.4.0.1: changed to 79 for left recursion
// 1.5.1.1: changed to 80 for trailing comma in array literals
// 1.5.2.5: changed to 84 for @import statements
%expect 84
// 1.5.4.0: changed to 84 for @import statements
// 1.5.4.4: changed to 86 for @doc statements
%expect 86

%token <sval> ID STRING_LIT CHAR_LIT
%token <ival> INT_VAL
Expand Down Expand Up @@ -137,7 +139,7 @@ a_Program g_program = NULL;
PUBLIC PROTECTED PRIVATE STATIC ABSTRACT CONST
SPORK ARROW_RIGHT ARROW_LEFT L_HACK R_HACK
GRUCK_RIGHT GRUCK_LEFT UNGRUCK_RIGHT UNGRUCK_LEFT
AT_OP AT_CTOR AT_DTOR AT_IMPORT
AT_OP AT_CTOR AT_DTOR AT_IMPORT AT_DOC


%type <program> program
Expand All @@ -159,6 +161,7 @@ a_Program g_program = NULL;
%type <stmt> jump_statement
%type <stmt> expression_statement
%type <stmt> import_statement
%type <stmt> doc_statement
%type <exp> expression
%type <exp> chuck_expression
%type <exp> arrow_expression
Expand Down Expand Up @@ -200,8 +203,10 @@ a_Program g_program = NULL;
%type <complex_exp> complex_exp
%type <polar_exp> polar_exp
%type <vec_exp> vec_exp // ge: added 1.3.5.3
%type <import> import_target // 1.5.2.5 (ge) added
%type <import> import_list // 1.5.2.5 (ge) added
%type <import> import_target // 1.5.4.0 (ge) added
%type <import> import_list // 1.5.4.0 (ge) added
%type <doc> doc_target // 1.5.4.4 (ge) added
%type <doc> doc_list // 1.5.4.4 (ge) added

%start program

Expand Down Expand Up @@ -361,6 +366,7 @@ statement
// | label_statement { }
| code_segment { $$ = $1; }
| import_statement { $$ = $1; }
| doc_statement { $$ = $1; }
;

jump_statement
Expand Down Expand Up @@ -402,17 +408,36 @@ code_segment
;

import_statement
: AT_IMPORT import_target { $$ = new_stmt_from_import( $2, @1.first_line, @1.first_column );}
| AT_IMPORT LBRACE import_list RBRACE { $$ = new_stmt_from_import( $3, @1.first_line, @1.first_column );}
: AT_IMPORT import_target { $$ = new_stmt_from_import( $2, @1.first_line, @1.first_column ); }
| AT_IMPORT LBRACK RBRACK { $$ = new_stmt_from_import( NULL, @1.first_line, @1.first_column ); }
| AT_IMPORT LBRACE RBRACE { $$ = new_stmt_from_import( NULL, @1.first_line, @1.first_column ); }
| AT_IMPORT LBRACK import_list LBRACK { $$ = new_stmt_from_import( $3, @1.first_line, @1.first_column ); }
| AT_IMPORT LBRACE import_list RBRACE { $$ = new_stmt_from_import( $3, @1.first_line, @1.first_column ); }
;

import_list
: import_target { $$ = $1; }
| import_target COMMA import_list { $$ = prepend_import( $1, $3, @1.first_line, @1.first_column ); }
;

import_target
: STRING_LIT { $$ = new_import( $1, NULL, @1.first_line, @1.first_column ); }
// | id_dot { $$ = new_import( NULL, $1, @1.first_line, @1.first_column ); }
// | id_dot { $$ = new_import( NULL, $1, @1.first_line, @1.first_column ); }
;

doc_statement
: AT_DOC doc_target { $$ = new_stmt_from_doc( $2, @1.first_line, @1.first_column ); }
| AT_DOC LBRACK doc_list RBRACK { $$ = new_stmt_from_doc( $3, @1.first_line, @1.first_column ); }
| AT_DOC LBRACE doc_list RBRACE { $$ = new_stmt_from_doc( $3, @1.first_line, @1.first_column ); }
;

doc_list
: doc_target { $$ = $1; }
| doc_target COMMA doc_list { $$ = prepend_doc( $1, $3, @1.first_line, @1.first_column ); }
;

doc_target
: STRING_LIT { $$ = new_doc( $1, @1.first_line, @1.first_column ); }
;

expression_statement
Expand Down
59 changes: 57 additions & 2 deletions src/core/chuck_absyn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ a_Stmt new_stmt_from_case( a_Exp exp, uint32_t lineNum, uint32_t posNum )
return a;
}

a_Stmt new_stmt_from_import( a_Import list, uint32_t line, uint32_t where ) // 1.5.2.5 (ge) added
a_Stmt new_stmt_from_import( a_Import list, uint32_t line, uint32_t where ) // 1.5.4.0 (ge) added
{
a_Stmt a = (a_Stmt)checked_malloc( sizeof(struct a_Stmt_) );
a->s_type = ae_stmt_import;
Expand All @@ -356,7 +356,19 @@ a_Stmt new_stmt_from_import( a_Import list, uint32_t line, uint32_t where ) // 1
return a;
}

a_Import new_import( c_str str, a_Id_List list, uint32_t line, uint32_t where ) // 1.5.2.5 (ge) added
a_Stmt new_stmt_from_doc( a_Doc list, uint32_t line, uint32_t where ) // 1.5.4.4 (ge) added
{
a_Stmt a = (a_Stmt)checked_malloc( sizeof(struct a_Stmt_) );
a->s_type = ae_stmt_doc;
a->stmt_doc.list = list;
a->line = line; a->where = where;
a->stmt_doc.line = line; a->stmt_doc.where = where;
a->stmt_doc.self = a;

return a;
}

a_Import new_import( c_str str, a_Id_List list, uint32_t line, uint32_t where ) // 1.5.4.0 (ge) added
{
a_Import a = (a_Import)checked_malloc( sizeof(struct a_Import_) );

Expand Down Expand Up @@ -404,6 +416,25 @@ a_Import prepend_import( a_Import target, a_Import list, uint32_t lineNum, uint3
return target;
}

a_Doc new_doc( c_str str, uint32_t line, uint32_t where ) // 1.5.4.4 (ge) added
{
a_Doc a = (a_Doc)checked_malloc( sizeof(struct a_Doc_) );

// copy allocated string pointer
a->desc = str; // no strdup( str ); <-- str should have been allocated in alloc_str()

// set line info
a->line = line; a->where = where;

return a;
}

a_Doc prepend_doc( a_Doc target, a_Doc list, uint32_t lineNum, uint32_t posNum )
{
target->next = list;
return target;
}

a_Exp append_expression( a_Exp list, a_Exp exp, uint32_t lineNum, uint32_t posNum )
{
a_Exp current;
Expand Down Expand Up @@ -1373,6 +1404,9 @@ void delete_stmt( a_Stmt stmt )
case ae_stmt_import:
delete_stmt_from_import( stmt );
break;
case ae_stmt_doc: // 1.5.4.4 (ge) added
delete_stmt_from_doc( stmt );
break;
}

CK_SAFE_FREE( stmt );
Expand Down Expand Up @@ -1476,6 +1510,27 @@ void delete_stmt_from_import( a_Stmt stmt )
}
}

void delete_stmt_from_doc( a_Stmt stmt )
{
EM_log( CK_LOG_FINEST, "deleting stmt %p (doc)...", (void *)stmt );

// pointer
a_Doc next = NULL, i = stmt->stmt_doc.list;

// iterate instead of recurse to avoid stack overflow
while( i )
{
// delete the content
CK_SAFE_FREE( i->desc );
// get next before we delete this one
next = i->next;
// delete the import target
CK_SAFE_FREE( i );
// move to the next one
i = next;
}
}

void delete_exp_from_primary( a_Exp_Primary_ & p )
{
EM_log( CK_LOG_FINEST, "deleting exp (primary)..." );
Expand Down
20 changes: 15 additions & 5 deletions src/core/chuck_absyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ typedef struct a_Stmt_Return_ * a_Stmt_Return;
typedef struct a_Stmt_Case_ * a_Stmt_Case;
typedef struct a_Stmt_GotoLabel_ * a_Stmt_GotoLabel;
typedef struct a_Stmt_Import_ * a_Stmt_Import;
typedef struct a_Stmt_Doc_ * a_Stmt_Doc;
typedef struct a_Decl_ * a_Decl;
typedef struct a_Var_Decl_ * a_Var_Decl;
typedef struct a_Var_Decl_List_ * a_Var_Decl_List;
Expand All @@ -153,7 +154,9 @@ typedef struct a_Ctor_Call_ * a_Ctor_Call; // 1.5.2.0 (ge) added
typedef struct a_Complex_ * a_Complex;
typedef struct a_Polar_ * a_Polar;
typedef struct a_Vec_ * a_Vec; // ge: added 1.3.5.3
typedef struct a_Import_ * a_Import; // 1.5.2.5 (ge) added
typedef struct a_Import_ * a_Import; // 1.5.4.0 (ge) added
typedef struct a_Doc_ * a_Doc; // 1.5.4.4 (ge) added


// forward reference for type
typedef struct Chuck_Type * t_CKTYPE;
Expand Down Expand Up @@ -192,6 +195,7 @@ a_Stmt new_stmt_from_return( a_Exp exp, uint32_t line, uint32_t where );
a_Stmt new_stmt_from_label( c_str xid, uint32_t line, uint32_t where );
a_Stmt new_stmt_from_case( a_Exp exp, uint32_t line, uint32_t where );
a_Stmt new_stmt_from_import( a_Import target, uint32_t line, uint32_t where );
a_Stmt new_stmt_from_doc( a_Doc target, uint32_t line, uint32_t where );
a_Exp append_expression( a_Exp list, a_Exp exp, uint32_t line, uint32_t where );
a_Exp new_exp_from_binary( a_Exp lhs, ae_Operator oper, a_Exp rhs, uint32_t line, uint32_t where );
a_Exp new_exp_from_unary( ae_Operator oper, a_Exp exp, uint32_t line, uint32_t where );
Expand Down Expand Up @@ -230,8 +234,10 @@ a_Array_Sub prepend_array_sub( a_Array_Sub array, a_Exp exp, uint32_t line, uint
a_Complex new_complex( a_Exp re, uint32_t line, uint32_t where );
a_Polar new_polar( a_Exp mod, uint32_t line, uint32_t where ); // ge: added 1.3.5.3
a_Vec new_vec( a_Exp e, uint32_t line, uint32_t where );
a_Import new_import( c_str str, a_Id_List id_list, uint32_t line, uint32_t where );
a_Import prepend_import( a_Import target, a_Import list, uint32_t line, uint32_t where );
a_Import new_import( c_str str, a_Id_List id_list, uint32_t line, uint32_t where ); // 1.5.4.0 (ge) added
a_Import prepend_import( a_Import target, a_Import list, uint32_t line, uint32_t where ); // 1.5.4.0 (ge) added
a_Doc new_doc( c_str str, uint32_t line, uint32_t where ); // 1.5.4.4 (ge) added
a_Doc prepend_doc( a_Doc target, a_Doc list, uint32_t line, uint32_t where ); // 1.5.4.4 (ge) added

a_Class_Def new_class_def( ae_Keyword class_decl, a_Id_List xid, a_Class_Ext ext, a_Class_Body body, uint32_t line, uint32_t where );
a_Class_Body new_class_body( a_Section section, uint32_t line, uint32_t where );
Expand Down Expand Up @@ -285,6 +291,7 @@ void delete_stmt_from_continue( a_Stmt stmt );
void delete_stmt_from_return( a_Stmt stmt );
void delete_stmt_from_label( a_Stmt stmt );
void delete_stmt_from_import( a_Stmt stmt );
void delete_stmt_from_doc( a_Stmt stmt );

// delete an exp
void delete_exp( a_Exp exp );
Expand Down Expand Up @@ -355,7 +362,8 @@ struct a_Arg_List_ { a_Type_Decl type_decl; a_Var_Decl var_decl; t_CKTYPE type;
struct a_Complex_ { a_Exp re; a_Exp im; uint32_t line; uint32_t where; a_Exp self; };
struct a_Polar_ { a_Exp mod; a_Exp phase; uint32_t line; uint32_t where; a_Exp self; };
struct a_Vec_ { a_Exp args; int numdims; uint32_t line; uint32_t where; a_Exp self; }; // ge: added 1.3.5.3
struct a_Import_ { c_str what; a_Import next; uint32_t line; uint32_t where; }; // 1.5.2.5 (ge) added
struct a_Import_ { c_str what; a_Import next; uint32_t line; uint32_t where; }; // 1.5.4.0 (ge) added
struct a_Doc_ { c_str desc; a_Doc next; uint32_t line; uint32_t where; }; // 1.5.4.4(ge) added

// enum primary exp type
typedef enum { ae_primary_var, ae_primary_num, ae_primary_float,
Expand Down Expand Up @@ -445,6 +453,7 @@ struct a_Stmt_Return_ { a_Exp val; uint32_t line; uint32_t where; a_Stmt self; }
struct a_Stmt_Case_ { a_Exp exp; uint32_t line; uint32_t where; a_Stmt self; };
struct a_Stmt_GotoLabel_ { S_Symbol name; uint32_t line; uint32_t where; a_Stmt self; };
struct a_Stmt_Import_ { a_Import list; uint32_t line; uint32_t where; a_Stmt self; };
struct a_Stmt_Doc_ { a_Doc list; uint32_t line; uint32_t where; a_Stmt self; };
struct a_Stmt_Code_
{
// statement list
Expand All @@ -459,7 +468,7 @@ struct a_Stmt_Code_
typedef enum { ae_stmt_exp, ae_stmt_while, ae_stmt_until, ae_stmt_for, ae_stmt_foreach,
ae_stmt_loop, ae_stmt_if, ae_stmt_code, ae_stmt_switch, ae_stmt_break,
ae_stmt_continue, ae_stmt_return, ae_stmt_case, ae_stmt_gotolabel,
ae_stmt_import
ae_stmt_import, ae_stmt_doc
} ae_Stmt_Type;

// a statement
Expand Down Expand Up @@ -493,6 +502,7 @@ struct a_Stmt_
struct a_Stmt_Case_ stmt_case;
struct a_Stmt_GotoLabel_ stmt_gotolabel;
struct a_Stmt_Import_ stmt_import;
struct a_Stmt_Doc_ stmt_doc;
};

// code position
Expand Down
18 changes: 9 additions & 9 deletions src/core/chuck_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ t_CKBOOL Chuck_Compiler::compile_entire_file( Chuck_Context * context )


//-----------------------------------------------------------------------------
// name: compile_import_only() // 1.5.2.5 (ge) added
// name: compile_import_only() | 1.5.4.0 (ge) added
// desc: import only public definitions (classes and operator overloads)
//-----------------------------------------------------------------------------
t_CKBOOL Chuck_Compiler::compile_import_only( Chuck_Context * context )
Expand Down Expand Up @@ -831,17 +831,17 @@ t_CKBOOL type_engine_scan_import( Chuck_Env * env, a_Stmt_List stmt_list,
if( stmt_list->stmt && stmt_list->stmt->s_type == ae_stmt_import )
{
// get the import list
a_Import import = stmt_list->stmt->stmt_import.list;
a_Import importList = stmt_list->stmt->stmt_import.list;
// loop over import list
while( import )
while( importList )
{
// resolve, using importer's absolute path, expandSearch == TRUE
string abs = compiler->resolveFilename( import->what, target->absolutePath, TRUE );
string abs = compiler->resolveFilename( importList->what, target->absolutePath, TRUE );
// if cannot resolve filename
if( abs == "" )
{
// print error
EM_error2( import->where, "no such file: '%s'", import->what );
EM_error2( importList->where, "no such file: '%s'", importList->what );
// done
return FALSE;
}
Expand All @@ -863,7 +863,7 @@ t_CKBOOL type_engine_scan_import( Chuck_Env * env, a_Stmt_List stmt_list,
if( !compiler->importChugin( abs, TRUE, theFile, errorStr ) )
{
// print error (chugin loading only prints to log)
EM_error2( import->where, "cannot load chugin: '%s'...", theFile.c_str() );
EM_error2( importList->where, "cannot load chugin: '%s'...", theFile.c_str() );
EM_error2( 0, "...(reason) %s", errorStr.length() ? errorStr.c_str() : "[none provided, unhelpfully (try running with more verbose log-level)]" );
EM_error2( 0, "...in file '%s'", abs.c_str() );
// error encountered in chugin load, bailing out
Expand All @@ -887,7 +887,7 @@ t_CKBOOL type_engine_scan_import( Chuck_Env * env, a_Stmt_List stmt_list,
// make new target with import only
t = new Chuck_CompileTarget( te_do_import_only );
// set filename, with transplant path, expand search if necessary
if( !compiler->openFile( t, import->what, target, TRUE, import->where ) )
if( !compiler->openFile( t, importList->what, target, TRUE, importList->where ) )
{
// clean up
CK_SAFE_DELETE( t );
Expand All @@ -899,9 +899,9 @@ t_CKBOOL type_engine_scan_import( Chuck_Env * env, a_Stmt_List stmt_list,
}
}
// add to target
target->dependencies.push_back( ImportTargetNode( t, import->where, import->line ) );
target->dependencies.push_back( ImportTargetNode( t, importList->where, importList->line ) );
// next in list
import = import->next;
importList = importList->next;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/chuck_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ struct Chuck_Compiler
// compile entire file
t_CKBOOL compile_entire_file( Chuck_Context * context );
// import only: public definitions (e.g., classes and operator overloads)
t_CKBOOL compile_import_only( Chuck_Context * context ); // 1.5.2.5 (ge) added
t_CKBOOL compile_import_only( Chuck_Context * context ); // 1.5.4.0 (ge) added
// all except import
t_CKBOOL compile_all_except_import( Chuck_Context * context );

Expand Down
7 changes: 6 additions & 1 deletion src/core/chuck_emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,12 @@ t_CKBOOL emit_engine_emit_stmt( Chuck_Emitter * emit, a_Stmt stmt, t_CKBOOL pop
break;
}

case ae_stmt_import: // 1.5.2.5 (ge) added
case ae_stmt_import: // 1.5.4.0 (ge) added
// do nothing here (return true to bypass)
ret = TRUE;
break;

case ae_stmt_doc: // 1.5.4.4 (ge) added
// do nothing here (return true to bypass)
ret = TRUE;
break;
Expand Down
Loading

0 comments on commit 0704a72

Please sign in to comment.