1 // Data structures used only in the zeta compiler (not at run time).
3 typedef struct Expr Expr
;
4 typedef struct Fnarg Fnarg
;
6 typedef struct Frame Frame
;
7 typedef struct Include Include
;
8 typedef struct Pos Pos
;
9 typedef struct Scope Scope
;
10 typedef struct Stmt Stmt
;
11 typedef struct Structelem Structelem
;
12 typedef struct Typecheck Typecheck
;
13 typedef struct Var Var
;
14 typedef struct Yystype Yystype
;
15 typedef struct Typex Typex
;
26 //////////////////////////////////////////////////////////////////////////
28 // Input, lexing, parsing
30 // Position of text in input (byte offsets).
35 int pos2fileline(Pos
, char**);
38 // %#P actual text at given location
42 // Error messages - error and warn are in the GNU C library, sigh.
43 #define error zeta_error
44 #define warn zeta_warn
46 void error(Pos
, char*, ...);
47 void warn(Pos
, char*, ...);
48 extern int nerrors
; // counts calls to error
51 // Convert zeta file name into zm, zo, prefix
54 Name
zeta2prefix(Name
);
56 typedef enum TypexOp
{
71 struct /*union, but safer*/ {
73 Type
*(*two
)(Type
*, Type
*);
74 Type
*(*listone
)(TypeL
*, Type
*);
75 Type
*(*list
)(TypeL
*);
84 Typex
*mkTypex(TypexOp
);
85 Typex
*mkTypex1(Type
*);
242 struct ParsePattern
*pattern
;
244 Expr
* mkExpr(ExprOp op
);
300 Stmt
* mkStmt(StmtOp op
);
311 Structelem
*mkStructelem(void);
339 Structelem
*structelem
;
340 StructelemL
*structeleml
;
344 struct GStmtL
*gstmtl
;
346 struct GRuleL
*grulel
;
348 struct GTermL
*gterml
;
349 struct ParsePattern
*parsepattern
;
350 struct PatToken
*pattoken
;
351 struct PatTokenL
*pattokenl
;
377 Fn
* mkFn(Name
, FnargL
*, Typex
*, StmtL
*);
387 Fnarg
* mkFnarg(Name
, Typex
*);
391 void pushinputfile(char*);
392 void pushinputstring(char*);
393 int yylex(Yystype
*, Pos
*);
394 Name
fullpath(char*);
396 extern int ingrammar
;
397 extern int intermname
;
398 extern int inbacktick
;
400 // List of input files read.
407 extern IncludeL
* includel
;
410 char* cescapestring(char *s
, int len
, int q
);
411 char* cunescapestring(char *s
, int len
);
412 int parsechar(char *s
, uint64
*value
, char **end
);
413 int parsefloat(char *s
, long double *value
);
414 int parseinteger(char *s
, uint64
*value
);
418 GType
* parsetype(char*);
420 extern Pos yylastpos
; // Pos of most recent yacc action
423 //////////////////////////////////////////////////////////////////////
425 // Type checking; more generally, semantic analysis
427 // A variable: has a name, a type, and a cname (used in C).
433 Scope
*scope
; // scope containing variable
436 Var
*redirect
; // use this one instead
437 Module
*module
; // when type == typemodule()
438 Type
*realtype
; // when type == typetype()
442 // Variables currently visible.
450 // Variables in an allocation frame (e.g.,
451 // all the top-level variables, or all the function
452 // arguments, or all the stack-allocated variables
453 // in this function.)
458 Fn
*fn
; // function containing frame
461 // Type-checking state.
464 int reachable
; // next statement is reachable
465 Scope
*scope
; // current variable scope
466 StmtL
*jumpl
; // all gotos and labels
467 int nlabel
; // labels used so far
468 StmtL
*breakl
; // stack of break-able statements
469 StmtL
*continuel
; // stack of continue-able statements
470 Fn
*fn
; // current function
471 FnL
*fnl
; // stack of fns containing fn.
474 // Expression contexts
477 Value
= 1<<0, // Expr being used for its value
478 LValue
= 1<<1, // Expr being used for its lvalue
479 BindValue
= 1<<2, // Expr must be a name
482 // Start/end new variable scope.
483 Scope
* pushscope(Typecheck
*);
484 void popscope(Typecheck
*, Scope
*);
486 // Copy an existing scope.
487 Scope
* copyscope(Scope
*);
489 // S1 and s2 were copied from scope
490 // and then changed. Merge common changes into scope.
491 void mergescopes(Scope
*s1
, Scope
*s2
, Scope
*scope
);
494 // Look for, declare a variable in a scope.
495 Var
* lookupvar(Scope
*scope
, Name name
);
496 VarL
* lookupvarl(Scope
*scope
, Name name
);
497 Var
* declarevar(Pos pos
, Scope
*scope
, Name name
, Type
*type
);
498 Var
* declarevarmany(Pos pos
, Scope
*scope
, Name name
, Type
*type
);
499 Var
* mlookupvar(Module
*m
, Name name
);
500 Var
* lookupattribute(Scope
*, Name
, Type
*);
501 Var
* _declarevar(Pos
, Scope
*, Name
, Type
*, int, Name
);
503 Name
attributename(Name
, Type
*);
504 Type
* attributeargtype(Type
*);
506 // Start/end new variable frame.
507 // Also starts/end new scope for the new frame.
508 Frame
* pushframe(Typecheck
*);
509 void popframe(Typecheck
*, Frame
*);
511 // Start/end new function to type check.
512 void pushfn(Typecheck
*, Fn
*);
513 void popfn(Typecheck
*, Fn
*);
515 // Type check a statement or expression.
516 void typecheckinit(void);
517 void typecheckstmt(Typecheck
*, Stmt
*);
518 int typecheckexpr(Typecheck
*, Expr
*, int);
519 void typecheckmatch(Typecheck
*, Expr
*, Type
*);
520 void typecheckcond(Typecheck
*, Expr
*, Scope
*, Scope
*);
521 int typecheckfn(Typecheck
*, Fn
*);
522 void typecheckgram(Typecheck
*, struct GTop
*);
523 int typecheckpexpr(Typecheck
*, Expr
*, int);
524 int typecheckgexpr(Typecheck
*, Expr
*, int);
526 Type
*typechecktype(Typecheck
*, Typex
*);
528 int typecheckpmatch(Typecheck
*, Expr
*, Type
*);
530 Type
*typedot(Pos
, Type
*, Name
);
531 Type
*typegramdot(Type
*, Name
);
533 void declaretype(Scope
*, Expr
*, Type
*);
534 void redeclare(Scope
*, Expr
*, Type
*);
536 Expr
* tobool(Typecheck
*, Expr
*);
537 void noassign(Expr
*, int);
538 void nosideeffect(Expr
*, int);
540 // List of all imported modules
541 extern ModuleL
* importl
;
543 // Top-level scope in module.
544 extern Scope
* topscope
;
547 ///////////////////////////////////////////////////////////////////
551 typedef struct Cbuf Cbuf
;
554 Fmt
*fmt
; // default output target for cgenstmt etc.
555 Fmt runfmt
; // output inside run() function
556 Fmt topfmt
; // output at top level
557 Fn
*fn
; // function being compiled
560 // %N ZN(N("string"))
563 void cbprint(Cbuf
*, Pos
, char*, ...);
564 void cgenstmt(Cbuf
*, Stmt
*);
565 void cgentypes(Cbuf
*);
566 void cgentypes0(Cbuf
*);
567 void cgenimportdecls(Cbuf
*);
568 void cgenstructdecls(Cbuf
*);
569 void cgengraminit(Cbuf
*);
570 Name
cgenexpr(Cbuf
*, Expr
*);
572 Name
cgenpexpr(Cbuf
*, Expr
*);
573 void cgenassign(Cbuf
*cb
, Expr
*dst
, Name src
);
574 void cgenpmatch(Cbuf
*cb
, Expr
*expr
, Name val
, int lfail
);
575 Name
csanitize(Name
);
577 void writemodule(Name
);
578 extern int linenumbers
;
580 Name
findinclude(Pos
, Name
);