History

Merge remote-tracking branch 'origin/main' into qt6-main

Spencer Salazars committed 3 years ago

Commit 2fbfa982 parents 0506c4a d0ee61c263 files · +29036 −10344Browse files

Showing the changes to src/core/chuck_parse.cpp (1 of 263 files). Show all

Unified · Split

src/core/chuck_parse.cpp+468−12
@@ -50,7 +50,7 @@ static const t_CKUINT MAX_FILENAME_LEN = 2048; · 49 unmodified lines
5050 static char g_filename[MAX_FILENAME_LEN+1] = "";
5151
5252 // external
53−extern "C" {
53+extern "C" {
5454 extern FILE *yyin;
5555 }
5656
@@ -101,15 +101,15 @@ FILE *win32_tmpfile() · 44 unmodified lines
101101 char tmp_path[MAX_PATH];
102102 char file_path[MAX_PATH];
103103 FILE * file = NULL;
104−
104+
105105 if(GetTempPath(256, tmp_path) == 0)
106106 return NULL;
107−
107+
108108 if(GetTempFileName(tmp_path, "mA", 0, file_path) == 0)
109109 return NULL;
110−
110+
111111 file = fopen(file_path, "wb+D");
112−
112+
113113 return file;
114114 }
115115
@@ -140,7 +140,7 @@ FILE * android_tmpfile() · 24 unmodified lines
140140 //-----------------------------------------------------------------------------
141141 t_CKBOOL chuck_parse( c_constr fname, FILE * fd, c_constr code )
142142 {
143− t_CKBOOL clo = FALSE;
143+ t_CKBOOL clo = FALSE;
144144 t_CKBOOL ret = FALSE;
145145
146146 // sanity check
@@ -192,7 +192,7 @@ t_CKBOOL chuck_parse( c_constr fname, FILE * fd, c_constr code ) · 45 unmodified lines
192192
193193 // parse
194194 if( !( yyparse() == 0 ) ) goto cleanup;
195−
195+
196196 // delete the lexer buffer
197197 yy_delete_buffer( ybs );
198198
@@ -211,7 +211,7 @@ t_CKBOOL chuck_parse( c_constr fname, FILE * fd, c_constr code ) · 12 unmodified lines
211211 }
212212 // remember filename
213213 strcpy( g_filename, fname );
214−
214+
215215 // test it
216216 if( !fd ) {
217217 fd = open_cat_ck( g_filename );
@@ -234,13 +234,13 @@ t_CKBOOL chuck_parse( c_constr fname, FILE * fd, c_constr code ) · 16 unmodified lines
234234
235235 // reset yyin to fd
236236 yyrestart( fd );
237−
237+
238238 // check
239239 if( yyin == NULL ) goto cleanup;
240−
240+
241241 // TODO: clean g_program
242242 g_program = NULL;
243−
243+
244244 // parse
245245 if( !(yyparse( ) == 0) ) goto cleanup;
246246
@@ -250,7 +250,7 @@ t_CKBOOL chuck_parse( c_constr fname, FILE * fd, c_constr code ) · 3 unmodified lines
250250 cleanup:
251251
252252 // done
253− if( clo ) fclose( fd );
253+ if( clo ) fclose( fd );
254254
255255 return ret;
256256 }
@@ -282,3 +282,459 @@ t_CKBOOL SyntaxQuery::parseLine( const std::string & line, SyntaxTokenList & tok · 25 unmodified lines
282282
283283 return TRUE;
284284 }
285+
286+
287+
288+
289+
290+//-----------------------------------------------------------------------------
291+// absyn to string functions
292+//-----------------------------------------------------------------------------
293+#include "chuck_type.h"
294+string absyn_exp2str( a_Exp exp, t_CKBOOL iterate = true );
295+string absyn_binary2str( a_Exp_Binary binary );
296+string absyn_unary2str( a_Exp_Unary unary );
297+string absyn_op2str( ae_Operator op );
298+string absyn_cast2str( a_Exp_Cast cast );
299+string absyn_postfix2str( a_Exp_Postfix postfix );
300+string absyn_dur2str( a_Exp_Dur dur );
301+string absyn_primary2str( a_Exp_Primary primary );
302+string absyn_array2str( a_Exp_Array array );
303+string absyn_func_call2str( a_Exp_Func_Call func_call );
304+string absyn_dot_member2str( a_Exp_Dot_Member dot_member );
305+string absyn_exp_if2str( a_Exp_If exp_if );
306+string absyn_decl2str( a_Exp_Decl decl );
307+
308+
309+
310+
311+//-----------------------------------------------------------------------------
312+// name: absyn2str()
313+// desc: convert abstract syntax exp to string
314+//-----------------------------------------------------------------------------
315+string absyn2str( a_Exp exp )
316+{
317+ return absyn_exp2str( exp ) + ";";
318+}
319+
320+
321+
322+
323+//-----------------------------------------------------------------------------
324+// name: name_demangle()
325+// desc: demangle chuck function names
326+// e.g., input: "version@0@Machine" output: "version"
327+//-----------------------------------------------------------------------------
328+string name_demangle( const string & name )
329+{
330+ // find the first @
331+ size_t pos = name.find_first_of('@');
332+ // if found, substring
333+ return ( pos != string::npos ) ? name.substr(0,pos) : name;
334+}
335+
336+
337+
338+
339+//-----------------------------------------------------------------------------
340+// name: absyn_exp2str()
341+// desc: convert abstract syntax exp to string
342+//-----------------------------------------------------------------------------
343+string absyn_exp2str( a_Exp exp, t_CKBOOL iterate )
344+{
345+ string str = "";
346+
347+ // loop over
348+ while( exp )
349+ {
350+ switch( exp->s_type )
351+ {
352+ case ae_exp_binary:
353+ str += absyn_binary2str( &exp->binary );
354+ break;
355+
356+ case ae_exp_unary:
357+ str += absyn_unary2str( &exp->unary );
358+ break;
359+
360+ case ae_exp_cast:
361+ str += absyn_cast2str( &exp->cast );
362+ break;
363+
364+ case ae_exp_postfix:
365+ str += absyn_postfix2str( &exp->postfix );
366+ break;
367+
368+ case ae_exp_dur:
369+ str += absyn_dur2str( &exp->dur );
370+ break;
371+
372+ case ae_exp_primary:
373+ str += absyn_primary2str( &exp->primary );
374+ break;
375+
376+ case ae_exp_array:
377+ str += absyn_array2str( &exp->array );
378+ break;
379+
380+ case ae_exp_func_call:
381+ str += absyn_func_call2str( &exp->func_call );
382+ break;
383+
384+ case ae_exp_dot_member:
385+ str += absyn_dot_member2str( &exp->dot_member );
386+ break;
387+
388+ case ae_exp_if:
389+ str += absyn_exp_if2str( &exp->exp_if );
390+ break;
391+
392+ case ae_exp_decl:
393+ str += absyn_decl2str( &exp->decl );
394+ break;
395+
396+ default:
397+ return "";
398+ }
399+
400+ // implicit cast
401+ if( exp->cast_to != NULL )
402+ break;
403+
404+ // break
405+ if( !iterate ) break;
406+
407+ // next
408+ exp = exp->next;
409+ // if more
410+ if( exp ) str += ",";
411+ }
412+
413+ return str;
414+}
415+
416+
417+
418+
419+//-----------------------------------------------------------------------------
420+// name: absyn_op2str()
421+// desc: convert abstract syntax operator to string
422+//-----------------------------------------------------------------------------
423+string absyn_op2str( ae_Operator op )
424+{
425+ string str;
426+ // get operation string
427+ switch( op )
428+ {
429+ case ae_op_plus: str = "+"; break;
430+ case ae_op_plus_chuck: str = "+=>"; break;
431+ case ae_op_minus: str = "-"; break;
432+ case ae_op_minus_chuck: str = "-=>"; break;
433+ case ae_op_times: str = "*"; break;
434+ case ae_op_times_chuck: str = "*=>"; break;
435+ case ae_op_divide: str = "/"; break;
436+ case ae_op_divide_chuck: str = "/=>"; break;
437+ case ae_op_s_or: str = "|"; break;
438+ case ae_op_s_or_chuck: str = "|=>"; break;
439+ case ae_op_s_and: str = "&"; break;
440+ case ae_op_s_and_chuck: str = "&=>"; break;
441+ case ae_op_shift_left: str = "<<"; break;
442+ case ae_op_shift_left_chuck: str = "<<=>"; break;
443+ case ae_op_shift_right: str = ">>"; break;
444+ case ae_op_shift_right_chuck: str = ">>=>"; break;
445+ case ae_op_percent: str = "%"; break;
446+ case ae_op_percent_chuck: str = "%=>"; break;
447+ case ae_op_s_xor: str = "^"; break;
448+ case ae_op_s_xor_chuck: str = "^=>"; break;
449+ case ae_op_chuck: str = "=>"; break;
450+ case ae_op_unchuck: str = "=<"; break;
451+ case ae_op_upchuck: str = "=^"; break;
452+ case ae_op_at_chuck: str = "@=>"; break;
453+ case ae_op_s_chuck: str = ""; break;
454+ case ae_op_eq: str = "=="; break;
455+ case ae_op_neq: str = "!="; break;
456+ case ae_op_lt: str = "<"; break;
457+ case ae_op_le: str = "<="; break;
458+ case ae_op_gt: str = ">"; break;
459+ case ae_op_ge: str = ">="; break;
460+ case ae_op_and: str = "&&"; break;
461+ case ae_op_or: str = "||"; break;
462+ case ae_op_arrow_left: str = "<-"; break;
463+ case ae_op_arrow_right: str = "->"; break;
464+
465+ // unary
466+ case ae_op_plusplus: str = "++"; break;
467+ case ae_op_minusminus: str = "--"; break;
468+ case ae_op_tilda: str = "~"; break;
469+ case ae_op_exclamation: str = "!"; break;
470+ case ae_op_spork: str = "spork ~"; break;
471+ case ae_op_new: str = "new"; break;
472+
473+ // not used
474+ case ae_op_typeof: str = "typeof"; break;
475+ case ae_op_sizeof: str = "sizeof"; break;
476+ default: str = "[OP]"; break;
477+ }
478+ return str;
479+}
480+
481+
482+
483+
484+//-----------------------------------------------------------------------------
485+// name: absyn_binary2str()
486+// desc: convert abstract syntax binary to string
487+//-----------------------------------------------------------------------------
488+string absyn_binary2str( a_Exp_Binary binary )
489+{
490+ string lhs = absyn_exp2str(binary->lhs);
491+ string op = absyn_op2str(binary->op);
492+ string rhs = absyn_exp2str(binary->rhs);
493+
494+ // use paren?
495+ bool paren = false;
496+ // spacing?
497+ bool spacing = false;
498+
499+ switch(binary->op)
500+ {
501+ case ae_op_plus_chuck:
502+ case ae_op_minus_chuck:
503+ case ae_op_times_chuck:
504+ case ae_op_divide_chuck:
505+ case ae_op_s_or_chuck:
506+ case ae_op_s_and_chuck:
507+ case ae_op_shift_left_chuck:
508+ case ae_op_shift_right_chuck:
509+ case ae_op_percent_chuck:
510+ case ae_op_s_xor_chuck:
511+ case ae_op_chuck:
512+ case ae_op_unchuck:
513+ case ae_op_upchuck:
514+ case ae_op_lt:
515+ case ae_op_gt:
516+ case ae_op_le:
517+ case ae_op_ge:
518+ case ae_op_at_chuck:
519+ paren = false;
520+ spacing = true;
521+ break;
522+ default:
523+ paren = true;
524+ spacing = false;
525+ break;
526+ }
527+
528+ // (lhs OP rhs)
529+ string str = string(paren?"(":"") + lhs + (spacing?" ":"") + op + (spacing?" ":"") + rhs + (paren?")":"");
530+ // return
531+ return str;
532+}
533+
534+
535+
536+
537+//-----------------------------------------------------------------------------
538+// name: absyn_unary2str()
539+// desc: convert abstract syntax unary to string
540+//-----------------------------------------------------------------------------
541+string absyn_unary2str( a_Exp_Unary unary )
542+{
543+ return absyn_op2str(unary->op) + " " + absyn_exp2str(unary->exp);
544+}
545+
546+
547+
548+
549+//-----------------------------------------------------------------------------
550+// name: absyn_cast2str()
551+// desc: convert abstract syntax cast to string
552+//-----------------------------------------------------------------------------
553+string absyn_cast2str( a_Exp_Cast cast )
554+{
555+ Chuck_Type * to = cast->self->type;
556+ return absyn_exp2str(cast->exp) + "$" + to->name;
557+}
558+
559+
560+
561+
562+//-----------------------------------------------------------------------------
563+// name: absyn_cast2str()
564+// desc: convert abstract syntax cast to string
565+//-----------------------------------------------------------------------------
566+string absyn_postfix2str( a_Exp_Postfix postfix )
567+{
568+ return absyn_exp2str( postfix->exp ) + absyn_op2str( postfix->op );
569+}
570+
571+
572+
573+
574+//-----------------------------------------------------------------------------
575+// name: absyn_dur2str()
576+// desc: convert abstract syntax dur to string
577+//-----------------------------------------------------------------------------
578+string absyn_dur2str( a_Exp_Dur dur )
579+{
580+ return absyn_exp2str(dur->base) + "::" + absyn_exp2str(dur->unit);
581+}
582+
583+
584+
585+
586+//-----------------------------------------------------------------------------
587+// name: absyn_primary2str()
588+// desc: convert abstract syntax primary to string
589+//-----------------------------------------------------------------------------
590+string absyn_primary2str( a_Exp_Primary primary )
591+{
592+ string str;
593+
594+ // find out exp
595+ switch( primary->s_type )
596+ {
597+ case ae_primary_var: str = S_name(primary->var); break;
598+ case ae_primary_num: str = to_string(primary->num); break;
599+ case ae_primary_float: str = to_string(primary->fnum); break;
600+ case ae_primary_str: str = string("\"") + primary->str + "\""; break;
601+ case ae_primary_char: str = string("'") + primary->chr + "'"; break;
602+ case ae_primary_array: str = "["+absyn_exp2str(primary->array->exp_list)+"]"; break;
603+ case ae_primary_complex: str = "#("+absyn_exp2str(primary->complex->re)+")"; break;
604+ case ae_primary_polar: str = "%("+absyn_exp2str(primary->polar->mod)+")"; break;
605+ case ae_primary_vec: str = "@("+absyn_exp2str(primary->vec->args)+")"; break;
606+ case ae_primary_exp: str = absyn_exp2str(primary->exp); break;
607+ case ae_primary_hack: str = "<<< " + absyn_exp2str(primary->exp) + " >>>"; break;
608+ case ae_primary_nil: str = "null"; break;
609+ default: str ="[PRIMARY]"; break;
610+ }
611+
612+ return str;
613+}
614+
615+
616+
617+
618+//-----------------------------------------------------------------------------
619+// name: absyn_array2str()
620+// desc: convert abstract syntax array to string
621+//-----------------------------------------------------------------------------
622+string absyn_array2str( a_Exp_Array array )
623+{
624+ string str;
625+ a_Array_Sub sub = NULL;
626+
627+ // get the sub
628+ sub = array->indices;
629+ // expressions
630+ a_Exp e = sub->exp_list;
631+
632+ // base of the array
633+ str = absyn_exp2str(array->base);
634+
635+ // loop over
636+ while( e )
637+ {
638+ // each index
639+ str += "[" + absyn_exp2str(e,false) + "]";
640+ // advance
641+ e = e->next;
642+ }
643+
644+ return str;
645+}
646+
647+
648+
649+
650+//-----------------------------------------------------------------------------
651+// name: absyn_func_call2str()
652+// desc: convert abstract syntax func_call to string
653+//-----------------------------------------------------------------------------
654+string absyn_func_call2str( a_Exp_Func_Call func_call )
655+{
656+ string str;
657+ // function name
658+ str = name_demangle( absyn_exp2str( func_call->func ) );
659+ // args
660+ str += "(" + absyn_exp2str( func_call->args ) + ")";
661+ // done
662+ return str;
663+}
664+
665+
666+
667+
668+//-----------------------------------------------------------------------------
669+// name: absyn_dot_member2str()
670+// desc: convert abstract syntax dot_member to string
671+//-----------------------------------------------------------------------------
672+string absyn_dot_member2str( a_Exp_Dot_Member dot_member )
673+{
674+ // return
675+ return absyn_exp2str(dot_member->base) + "." + name_demangle(S_name(dot_member->xid));
676+}
677+
678+
679+
680+
681+//-----------------------------------------------------------------------------
682+// name: absyn_exp_if2str()
683+// desc: convert abstract syntax exp_if to string
684+//-----------------------------------------------------------------------------
685+string absyn_exp_if2str( a_Exp_If exp_if )
686+{
687+ return "("+absyn_exp2str(exp_if->cond) + "?" + absyn_exp2str(exp_if->if_exp)
688+ + ":" + absyn_exp2str(exp_if->else_exp)+")";
689+}
690+
691+
692+
693+
694+//-----------------------------------------------------------------------------
695+// name: absyn_decl2str()
696+// desc: convert abstract syntax decl to string
697+//-----------------------------------------------------------------------------
698+string absyn_decl2str( a_Exp_Decl decl )
699+{
700+ a_Var_Decl_List list = decl->var_decl_list;
701+ string str;
702+ a_Exp e = NULL;
703+
704+ if( decl->is_global ) str += "global ";
705+ else if( decl->is_static ) str += "static ";
706+
707+ while( list )
708+ {
709+ // type
710+ str += list->var_decl->value->type->name + " " + (decl->type->ref && !list->var_decl->array ? "@ " : "") + list->var_decl->value->name;
711+ // array?
712+ if( list->var_decl->array )
713+ {
714+ e = list->var_decl->array->exp_list;
715+ if( e )
716+ {
717+ // loop over
718+ while( e )
719+ {
720+ // each index
721+ str += "[" + absyn_exp2str(e,false) + "]";
722+ // advance
723+ e = e->next;
724+ }
725+ }
726+ else
727+ {
728+ for( t_CKINT i = 0; i < list->var_decl->value->type->array_depth; i++ )
729+ {
730+ str += "[]";
731+ }
732+ }
733+ }
734+
735+ list = list->next;
736+ if( list ) str += ",";
737+ }
738+
739+ return str;
740+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t