PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/iparser/igo_parser.y

https://github.com/zsaleeba/indigo
Happy | 2242 lines | 2091 code | 151 blank | 0 comment | 0 complexity | 3b3383b98dd0b5c283ad9f981ac6406f MD5 | raw file
  1. // Copyright 2009 The Go Authors and the Indigo authors.
  2. // All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. /*
  6. * Go language grammar.
  7. *
  8. * The Go semicolon rules are:
  9. *
  10. * 1. all statements and declarations are terminated by semicolons.
  11. * 2. semicolons can be omitted before a closing ) or }.
  12. * 3. semicolons are inserted by the lexer before a newline
  13. * following a specific list of tokens.
  14. *
  15. * Rules #1 and #2 are accomplished by writing the lists as
  16. * semicolon-separated lists with an optional trailing semicolon.
  17. * Rule #3 is implemented in yylex.
  18. */
  19. %{
  20. package parser
  21. import (
  22. // "bufio"
  23. // "fmt"
  24. // "math"
  25. // "os"
  26. "github.com/zsaleeba/indigo/ast"
  27. )
  28. %}
  29. %union {
  30. node ast.Node
  31. list *ast.NodeList
  32. typ ast.Type
  33. sym *ast.Sym
  34. val ast.Val
  35. i int
  36. }
  37. // |sed 's/.* //' |9 fmt -l1 |sort |9 fmt -l50 | sed 's/^/%xxx /'
  38. %token <val> LLITERAL
  39. %token <sym> LBREAK LCASE LCHAN LCONST LCONTINUE LDDD
  40. %token <sym> LDEFAULT LDEFER LELSE LFALL LFOR LFUNC LGO LGOTO
  41. %token <sym> LIF LIMPORT LINTERFACE LMAP LNAME
  42. %token <sym> LPACKAGE LRANGE LRETURN LSELECT LSTRUCT LSWITCH
  43. %token <sym> LTYPE LVAR
  44. %token LANDAND LANDNOT LBODY LCOMM LDEC LEQ LGE LGT
  45. %token LIGNORE LINC LLE LLSH LLT LNE LOROR LRSH
  46. %token LADDAS LSUBAS LMULAS LDIVAS LANDNOTAS LANDAS LORAS
  47. %token LLSHAS LRSHAS LDECLAS
  48. %type <i> lbrace import_here
  49. %type <sym> sym packname
  50. %type <val> oliteral
  51. %type <node> stmt ntype
  52. %type <node> arg_type
  53. %type <node> case caseblock
  54. %type <node> compound_stmt dotname embed expr complitexpr bare_complitexpr
  55. %type <node> expr_or_type
  56. %type <node> fndcl hidden_fndcl fnliteral
  57. %type <node> for_body for_header for_stmt if_header if_stmt non_dcl_stmt
  58. %type <node> interfacedcl keyval labelname name
  59. %type <node> name_or_type non_expr_type
  60. %type <node> new_name dcl_name oexpr typedclname
  61. %type <node> onew_name
  62. %type <node> osimple_stmt pexpr pexpr_no_paren
  63. %type <node> pseudocall range_stmt select_stmt
  64. %type <node> simple_stmt
  65. %type <node> switch_stmt uexpr
  66. %type <node> xfndcl typedcl start_complit
  67. %type <list> xdcl fnbody fnres loop_body dcl_name_list
  68. %type <list> new_name_list expr_list keyval_list braced_keyval_list expr_or_type_list xdcl_list
  69. %type <list> oexpr_list caseblock_list elseif elseif_list else stmt_list oarg_type_list_ocomma arg_type_list
  70. %type <list> interfacedcl_list vardcl vardcl_list structdcl structdcl_list
  71. %type <list> common_dcl constdcl constdcl1 constdcl_list typedcl_list
  72. %type <node> convtype comptype dotdotdot
  73. %type <node> indcl interfacetype structtype ptrtype
  74. %type <node> recvchantype non_recvchantype othertype fnret_type fntype
  75. %type <sym> hidden_importsym hidden_pkg_importsym
  76. %type <node> hidden_constant hidden_literal hidden_funarg
  77. %type <node> hidden_interfacedcl hidden_structdcl
  78. %type <list> hidden_funres
  79. %type <list> ohidden_funres
  80. %type <list> hidden_funarg_list ohidden_funarg_list
  81. %type <list> hidden_interfacedcl_list ohidden_interfacedcl_list
  82. %type <list> hidden_structdcl_list ohidden_structdcl_list
  83. %type <typ> hidden_type hidden_type_misc hidden_pkgtype
  84. %type <typ> hidden_type_func
  85. %type <typ> hidden_type_recv_chan hidden_type_non_recv_chan
  86. %left LCOMM /* outside the usual hierarchy; here for good error messages */
  87. %left LOROR
  88. %left LANDAND
  89. %left LEQ LNE LLE LGE LLT LGT
  90. %left '+' '-' '|' '^'
  91. %left '*' '/' '%' '&' LLSH LRSH LANDNOT
  92. /*
  93. * manual override of shift/reduce conflicts.
  94. * the general form is that we assign a precedence
  95. * to the token being shifted and then introduce
  96. * NotToken with lower precedence or PreferToToken with higher
  97. * and annotate the reducing rule accordingly.
  98. */
  99. %left NotPackage
  100. %left LPACKAGE
  101. %left NotParen
  102. %left '('
  103. %left ')'
  104. %left PreferToRightParen
  105. %%
  106. file:
  107. loadsys
  108. package
  109. imports
  110. xdcl_list
  111. {
  112. // xtop = concat(xtop, $4);
  113. }
  114. package:
  115. %prec NotPackage
  116. {
  117. // prevlineno = lineno;
  118. // yyerror("package statement must be first");
  119. // errorexit();
  120. }
  121. | LPACKAGE sym ';'
  122. {
  123. // mkpackage($2.name);
  124. }
  125. /*
  126. * this loads the definitions for the low-level runtime functions,
  127. * so that the compiler can generate calls to them,
  128. * but does not make the name "runtime" visible as a package.
  129. */
  130. loadsys:
  131. {
  132. // importpkg = runtimepkg;
  133. //
  134. // if(debug['A'])
  135. // cannedimports("runtime.builtin", "package runtime\n\n$$\n\n");
  136. // else
  137. // cannedimports("runtime.builtin", runtimeimport);
  138. // curio.importsafe = 1;
  139. }
  140. import_package
  141. import_there
  142. {
  143. // importpkg = nil;
  144. }
  145. imports:
  146. | imports import ';'
  147. import:
  148. LIMPORT import_stmt
  149. | LIMPORT '(' import_stmt_list osemi ')'
  150. | LIMPORT '(' ')'
  151. import_stmt:
  152. import_here import_package import_there
  153. {
  154. // Pkg *ipkg;
  155. // Sym *my;
  156. // Node *pack;
  157. //
  158. // ipkg = importpkg;
  159. // my = importmyname;
  160. // importpkg = nil;
  161. // importmyname = S;
  162. //
  163. // if(my == nil)
  164. // my = lookup(ipkg->name);
  165. //
  166. // pack = nod(OPACK, N, N);
  167. // pack->sym = my;
  168. // pack->pkg = ipkg;
  169. // pack->lineno = $1;
  170. //
  171. // if(my->name[0] == '.') {
  172. // importdot(ipkg, pack);
  173. // break;
  174. // }
  175. // if(strcmp(my->name, "init") == 0) {
  176. // yyerror("cannot import package as init - init must be a func");
  177. // break;
  178. // }
  179. // if(my->name[0] == '_' && my->name[1] == '\0')
  180. // break;
  181. // if(my->def) {
  182. // lineno = $1;
  183. // redeclare(my, "as imported package name");
  184. // }
  185. // my->def = pack;
  186. // my->lastlineno = $1;
  187. // my->block = 1; // at top level
  188. }
  189. | import_here import_there
  190. {
  191. // When an invalid import path is passed to importfile,
  192. // it calls yyerror and then sets up a fake import with
  193. // no package statement. This allows us to test more
  194. // than one invalid import statement in a single file.
  195. // if(nerrors == 0)
  196. // fatal("phase error in import");
  197. }
  198. import_stmt_list:
  199. import_stmt
  200. | import_stmt_list ';' import_stmt
  201. import_here:
  202. LLITERAL
  203. {
  204. // // import with original name
  205. // $$ = parserline();
  206. // importmyname = S;
  207. // importfile(&$1, $$);
  208. }
  209. | sym LLITERAL
  210. {
  211. // // import with given name
  212. // $$ = parserline();
  213. // importmyname = $1;
  214. // importfile(&$2, $$);
  215. }
  216. | '.' LLITERAL
  217. {
  218. // // import into my name space
  219. // $$ = parserline();
  220. // importmyname = lookup(".");
  221. // importfile(&$2, $$);
  222. }
  223. import_package:
  224. LPACKAGE LNAME import_safety ';'
  225. {
  226. // if(importpkg->name == nil) {
  227. // importpkg->name = $2->name;
  228. // pkglookup($2->name, nil)->npkg++;
  229. // } else if(strcmp(importpkg->name, $2->name) != 0)
  230. // yyerror("conflicting names %s and %s for package \"%Z\"", importpkg->name, $2->name, importpkg->path);
  231. // importpkg->direct = 1;
  232. // importpkg->safe = curio.importsafe;
  233. //
  234. // if(safemode && !curio.importsafe)
  235. // yyerror("cannot import unsafe package \"%Z\"", importpkg->path);
  236. }
  237. import_safety:
  238. | LNAME
  239. {
  240. // if(strcmp($1->name, "safe") == 0)
  241. // curio.importsafe = 1;
  242. }
  243. import_there:
  244. {
  245. // defercheckwidth();
  246. }
  247. hidden_import_list '$' '$'
  248. {
  249. // resumecheckwidth();
  250. // unimportfile();
  251. }
  252. /*
  253. * declarations
  254. */
  255. xdcl:
  256. {
  257. // yyerror("empty top-level declaration");
  258. // $$ = nil;
  259. }
  260. | common_dcl
  261. | xfndcl
  262. {
  263. // $$ = list1($1);
  264. }
  265. | non_dcl_stmt
  266. {
  267. // yyerror("non-declaration statement outside function body");
  268. // $$ = nil;
  269. }
  270. | error
  271. {
  272. // $$ = nil;
  273. }
  274. common_dcl:
  275. LVAR vardcl
  276. {
  277. // $$ = $2;
  278. }
  279. | LVAR '(' vardcl_list osemi ')'
  280. {
  281. // $$ = $3;
  282. }
  283. | LVAR '(' ')'
  284. {
  285. // $$ = nil;
  286. }
  287. | lconst constdcl
  288. {
  289. // $$ = $2;
  290. // iota = -100000;
  291. // lastconst = nil;
  292. }
  293. | lconst '(' constdcl osemi ')'
  294. {
  295. // $$ = $3;
  296. // iota = -100000;
  297. // lastconst = nil;
  298. }
  299. | lconst '(' constdcl ';' constdcl_list osemi ')'
  300. {
  301. // $$ = concat($3, $5);
  302. // iota = -100000;
  303. // lastconst = nil;
  304. }
  305. | lconst '(' ')'
  306. {
  307. // $$ = nil;
  308. // iota = -100000;
  309. }
  310. | LTYPE typedcl
  311. {
  312. // $$ = list1($2);
  313. }
  314. | LTYPE '(' typedcl_list osemi ')'
  315. {
  316. // $$ = $3;
  317. }
  318. | LTYPE '(' ')'
  319. {
  320. // $$ = nil;
  321. }
  322. lconst:
  323. LCONST
  324. {
  325. // iota = 0;
  326. }
  327. vardcl:
  328. dcl_name_list ntype
  329. {
  330. // $$ = variter($1, $2, nil);
  331. }
  332. | dcl_name_list ntype '=' expr_list
  333. {
  334. // $$ = variter($1, $2, $4);
  335. }
  336. | dcl_name_list '=' expr_list
  337. {
  338. // $$ = variter($1, nil, $3);
  339. }
  340. constdcl:
  341. dcl_name_list ntype '=' expr_list
  342. {
  343. // $$ = constiter($1, $2, $4);
  344. }
  345. | dcl_name_list '=' expr_list
  346. {
  347. // $$ = constiter($1, N, $3);
  348. }
  349. constdcl1:
  350. constdcl
  351. | dcl_name_list ntype
  352. {
  353. // $$ = constiter($1, $2, nil);
  354. }
  355. | dcl_name_list
  356. {
  357. // $$ = constiter($1, N, nil);
  358. }
  359. typedclname:
  360. sym
  361. {
  362. // // different from dclname because the name
  363. // // becomes visible right here, not at the end
  364. // // of the declaration.
  365. // $$ = typedcl0($1);
  366. }
  367. typedcl:
  368. typedclname ntype
  369. {
  370. // $$ = typedcl1($1, $2, 1);
  371. }
  372. simple_stmt:
  373. expr
  374. {
  375. // $$ = $1;
  376. //
  377. // // These nodes do not carry line numbers.
  378. // // Since a bare name used as an expression is an error,
  379. // // introduce a wrapper node to give the correct line.
  380. // switch($$->op) {
  381. // case ONAME:
  382. // case ONONAME:
  383. // case OTYPE:
  384. // case OPACK:
  385. // case OLITERAL:
  386. // $$ = nod(OPAREN, $$, N);
  387. // $$->implicit = 1;
  388. // break;
  389. // }
  390. }
  391. | expr LADDAS expr
  392. {
  393. // $$ = nod(OASOP, $1, $3);
  394. // $$->etype = $2; // rathole to pass opcode
  395. }
  396. | expr LSUBAS expr
  397. {
  398. }
  399. | expr LMULAS expr
  400. {
  401. }
  402. | expr LDIVAS expr
  403. {
  404. }
  405. | expr LANDNOTAS expr
  406. {
  407. }
  408. | expr LANDAS expr
  409. {
  410. }
  411. | expr LORAS expr
  412. {
  413. }
  414. | expr LLSHAS expr
  415. {
  416. }
  417. | expr LRSHAS expr
  418. {
  419. }
  420. | expr_list '=' expr_list
  421. {
  422. // if($1->next == nil && $3->next == nil) {
  423. // // simple
  424. // $$ = nod(OAS, $1->n, $3->n);
  425. // break;
  426. // }
  427. // // multiple
  428. // $$ = nod(OAS2, N, N);
  429. // $$->list = $1;
  430. // $$->rlist = $3;
  431. }
  432. | expr_list LDECLAS expr_list
  433. {
  434. // if($3->n->op == OTYPESW) {
  435. // $$ = nod(OTYPESW, N, $3->n->right);
  436. // if($3->next != nil)
  437. // yyerror("expr.(type) must be alone in list");
  438. // if($1->next != nil)
  439. // yyerror("argument count mismatch: %d = %d", count($1), 1);
  440. // else if(($1->n->op != ONAME && $1->n->op != OTYPE && $1->n->op != ONONAME) || isblank($1->n))
  441. // yyerror("invalid variable name %N in type switch", $1->n);
  442. // else
  443. // $$->left = dclname($1->n->sym); // it's a colas, so must not re-use an oldname.
  444. // break;
  445. // }
  446. // $$ = colas($1, $3, $2);
  447. }
  448. | expr LINC
  449. {
  450. // $$ = nod(OASOP, $1, nodintconst(1));
  451. // $$->etype = OADD;
  452. }
  453. | expr LDEC
  454. {
  455. // $$ = nod(OASOP, $1, nodintconst(1));
  456. // $$->etype = OSUB;
  457. }
  458. case:
  459. LCASE expr_or_type_list ':'
  460. {
  461. // Node *n, *nn;
  462. //
  463. // // will be converted to OCASE
  464. // // right will point to next case
  465. // // done in casebody()
  466. // markdcl();
  467. // $$ = nod(OXCASE, N, N);
  468. // $$->list = $2;
  469. // if(typesw != N && typesw->right != N && (n=typesw->right->left) != N) {
  470. // // type switch - declare variable
  471. // nn = newname(n->sym);
  472. // declare(nn, dclcontext);
  473. // $$->nname = nn;
  474. //
  475. // // keep track of the instances for reporting unused
  476. // nn->defn = typesw->right;
  477. // }
  478. }
  479. | LCASE expr_or_type_list '=' expr ':'
  480. {
  481. // Node *n;
  482. //
  483. // // will be converted to OCASE
  484. // // right will point to next case
  485. // // done in casebody()
  486. // markdcl();
  487. // $$ = nod(OXCASE, N, N);
  488. // if($2->next == nil)
  489. // n = nod(OAS, $2->n, $4);
  490. // else {
  491. // n = nod(OAS2, N, N);
  492. // n->list = $2;
  493. // n->rlist = list1($4);
  494. // }
  495. // $$->list = list1(n);
  496. }
  497. | LCASE expr_or_type_list LDECLAS expr ':'
  498. {
  499. // // will be converted to OCASE
  500. // // right will point to next case
  501. // // done in casebody()
  502. // markdcl();
  503. // $$ = nod(OXCASE, N, N);
  504. // $$->list = list1(colas($2, list1($4), $3));
  505. }
  506. | LDEFAULT ':'
  507. {
  508. // Node *n, *nn;
  509. //
  510. // markdcl();
  511. // $$ = nod(OXCASE, N, N);
  512. // if(typesw != N && typesw->right != N && (n=typesw->right->left) != N) {
  513. // // type switch - declare variable
  514. // nn = newname(n->sym);
  515. // declare(nn, dclcontext);
  516. // $$->nname = nn;
  517. //
  518. // // keep track of the instances for reporting unused
  519. // nn->defn = typesw->right;
  520. // }
  521. }
  522. compound_stmt:
  523. '{'
  524. {
  525. // markdcl();
  526. }
  527. stmt_list '}'
  528. {
  529. // if($3 == nil)
  530. // $$ = nod(OEMPTY, N, N);
  531. // else
  532. // $$ = liststmt($3);
  533. // popdcl();
  534. }
  535. caseblock:
  536. case
  537. {
  538. // // If the last token read by the lexer was consumed
  539. // // as part of the case, clear it (parser has cleared yychar).
  540. // // If the last token read by the lexer was the lookahead
  541. // // leave it alone (parser has it cached in yychar).
  542. // // This is so that the stmt_list action doesn't look at
  543. // // the case tokens if the stmt_list is empty.
  544. // yylast = yychar;
  545. // $1->xoffset = block;
  546. }
  547. stmt_list
  548. {
  549. // int last;
  550. //
  551. // // This is the only place in the language where a statement
  552. // // list is not allowed to drop the final semicolon, because
  553. // // it's the only place where a statement list is not followed
  554. // // by a closing brace. Handle the error for pedantry.
  555. //
  556. // // Find the final token of the statement list.
  557. // // yylast is lookahead; yyprev is last of stmt_list
  558. // last = yyprev;
  559. //
  560. // if(last > 0 && last != ';' && yychar != '}')
  561. // yyerror("missing statement after label");
  562. // $$ = $1;
  563. // $$->nbody = $3;
  564. // popdcl();
  565. }
  566. caseblock_list:
  567. {
  568. // $$ = nil;
  569. }
  570. | caseblock_list caseblock
  571. {
  572. // $$ = list($1, $2);
  573. }
  574. loop_body:
  575. LBODY
  576. {
  577. // markdcl();
  578. }
  579. stmt_list '}'
  580. {
  581. // $$ = $3;
  582. // popdcl();
  583. }
  584. range_stmt:
  585. expr_list '=' LRANGE expr
  586. {
  587. // $$ = nod(ORANGE, N, $4);
  588. // $$->list = $1;
  589. // $$->etype = 0; // := flag
  590. }
  591. | expr_list LDECLAS LRANGE expr
  592. {
  593. // $$ = nod(ORANGE, N, $4);
  594. // $$->list = $1;
  595. // $$->colas = 1;
  596. // colasdefn($1, $$);
  597. }
  598. for_header:
  599. osimple_stmt ';' osimple_stmt ';' osimple_stmt
  600. {
  601. // // init ; test ; incr
  602. // if($5 != N && $5->colas != 0)
  603. // yyerror("cannot declare in the for-increment");
  604. // $$ = nod(OFOR, N, N);
  605. // if($1 != N)
  606. // $$->ninit = list1($1);
  607. // $$->ntest = $3;
  608. // $$->nincr = $5;
  609. }
  610. | osimple_stmt
  611. {
  612. // // normal test
  613. // $$ = nod(OFOR, N, N);
  614. // $$->ntest = $1;
  615. }
  616. | range_stmt
  617. for_body:
  618. for_header loop_body
  619. {
  620. // $$ = $1;
  621. // $$->nbody = concat($$->nbody, $2);
  622. }
  623. for_stmt:
  624. LFOR
  625. {
  626. // markdcl();
  627. }
  628. for_body
  629. {
  630. // $$ = $3;
  631. // popdcl();
  632. }
  633. if_header:
  634. osimple_stmt
  635. {
  636. // // test
  637. // $$ = nod(OIF, N, N);
  638. // $$->ntest = $1;
  639. }
  640. | osimple_stmt ';' osimple_stmt
  641. {
  642. // // init ; test
  643. // $$ = nod(OIF, N, N);
  644. // if($1 != N)
  645. // $$->ninit = list1($1);
  646. // $$->ntest = $3;
  647. }
  648. /* IF cond body (ELSE IF cond body)* (ELSE block)? */
  649. if_stmt:
  650. LIF
  651. {
  652. // markdcl();
  653. }
  654. if_header
  655. {
  656. // if($3->ntest == N)
  657. // yyerror("missing condition in if statement");
  658. }
  659. loop_body
  660. {
  661. // $3->nbody = $5;
  662. }
  663. elseif_list else
  664. {
  665. // Node *n;
  666. // NodeList *nn;
  667. //
  668. // $$ = $3;
  669. // n = $3;
  670. // popdcl();
  671. // for(nn = concat($7, $8); nn; nn = nn->next) {
  672. // if(nn->n->op == OIF)
  673. // popdcl();
  674. // n->nelse = list1(nn->n);
  675. // n = nn->n;
  676. // }
  677. }
  678. elseif:
  679. LELSE LIF
  680. {
  681. // markdcl();
  682. }
  683. if_header loop_body
  684. {
  685. // if($4->ntest == N)
  686. // yyerror("missing condition in if statement");
  687. // $4->nbody = $5;
  688. // $$ = list1($4);
  689. }
  690. elseif_list:
  691. {
  692. // $$ = nil;
  693. }
  694. | elseif_list elseif
  695. {
  696. // $$ = concat($1, $2);
  697. }
  698. else:
  699. {
  700. // $$ = nil;
  701. }
  702. | LELSE compound_stmt
  703. {
  704. // NodeList *node;
  705. //
  706. // node = mal(sizeof *node);
  707. // node->n = $2;
  708. // node->end = node;
  709. // $$ = node;
  710. }
  711. switch_stmt:
  712. LSWITCH
  713. {
  714. // markdcl();
  715. }
  716. if_header
  717. {
  718. // Node *n;
  719. // n = $3->ntest;
  720. // if(n != N && n->op != OTYPESW)
  721. // n = N;
  722. // typesw = nod(OXXX, typesw, n);
  723. }
  724. LBODY caseblock_list '}'
  725. {
  726. // $$ = $3;
  727. // $$->op = OSWITCH;
  728. // $$->list = $6;
  729. // typesw = typesw->left;
  730. // popdcl();
  731. }
  732. select_stmt:
  733. LSELECT
  734. {
  735. // typesw = nod(OXXX, typesw, N);
  736. }
  737. LBODY caseblock_list '}'
  738. {
  739. // $$ = nod(OSELECT, N, N);
  740. // $$->lineno = typesw->lineno;
  741. // $$->list = $4;
  742. // typesw = typesw->left;
  743. }
  744. /*
  745. * expressions
  746. */
  747. expr:
  748. uexpr
  749. | expr LOROR expr
  750. {
  751. // $$ = nod(OOROR, $1, $3);
  752. }
  753. | expr LANDAND expr
  754. {
  755. // $$ = nod(OANDAND, $1, $3);
  756. }
  757. | expr LEQ expr
  758. {
  759. // $$ = nod(OEQ, $1, $3);
  760. }
  761. | expr LNE expr
  762. {
  763. // $$ = nod(ONE, $1, $3);
  764. }
  765. | expr LLT expr
  766. {
  767. // $$ = nod(OLT, $1, $3);
  768. }
  769. | expr LLE expr
  770. {
  771. // $$ = nod(OLE, $1, $3);
  772. }
  773. | expr LGE expr
  774. {
  775. // $$ = nod(OGE, $1, $3);
  776. }
  777. | expr LGT expr
  778. {
  779. // $$ = nod(OGT, $1, $3);
  780. }
  781. | expr '+' expr
  782. {
  783. // $$ = nod(OADD, $1, $3);
  784. }
  785. | expr '-' expr
  786. {
  787. // $$ = nod(OSUB, $1, $3);
  788. }
  789. | expr '|' expr
  790. {
  791. // $$ = nod(OOR, $1, $3);
  792. }
  793. | expr '^' expr
  794. {
  795. // $$ = nod(OXOR, $1, $3);
  796. }
  797. | expr '*' expr
  798. {
  799. // $$ = nod(OMUL, $1, $3);
  800. }
  801. | expr '/' expr
  802. {
  803. // $$ = nod(ODIV, $1, $3);
  804. }
  805. | expr '%' expr
  806. {
  807. // $$ = nod(OMOD, $1, $3);
  808. }
  809. | expr '&' expr
  810. {
  811. // $$ = nod(OAND, $1, $3);
  812. }
  813. | expr LANDNOT expr
  814. {
  815. // $$ = nod(OANDNOT, $1, $3);
  816. }
  817. | expr LLSH expr
  818. {
  819. // $$ = nod(OLSH, $1, $3);
  820. }
  821. | expr LRSH expr
  822. {
  823. // $$ = nod(ORSH, $1, $3);
  824. }
  825. /* not an expression anymore, but left in so we can give a good error */
  826. | expr LCOMM expr
  827. {
  828. // $$ = nod(OSEND, $1, $3);
  829. }
  830. uexpr:
  831. pexpr
  832. | '*' uexpr
  833. {
  834. // $$ = nod(OIND, $2, N);
  835. }
  836. | '&' uexpr
  837. {
  838. // if($2->op == OCOMPLIT) {
  839. // // Special case for &T{...}: turn into (*T){...}.
  840. // $$ = $2;
  841. // $$->right = nod(OIND, $$->right, N);
  842. // $$->right->implicit = 1;
  843. // } else {
  844. // $$ = nod(OADDR, $2, N);
  845. // }
  846. }
  847. | '+' uexpr
  848. {
  849. // $$ = nod(OPLUS, $2, N);
  850. }
  851. | '-' uexpr
  852. {
  853. // $$ = nod(OMINUS, $2, N);
  854. }
  855. | '!' uexpr
  856. {
  857. // $$ = nod(ONOT, $2, N);
  858. }
  859. | '~' uexpr
  860. {
  861. // yyerror("the bitwise complement operator is ^");
  862. // $$ = nod(OCOM, $2, N);
  863. }
  864. | '^' uexpr
  865. {
  866. // $$ = nod(OCOM, $2, N);
  867. }
  868. | LCOMM uexpr
  869. {
  870. // $$ = nod(ORECV, $2, N);
  871. }
  872. /*
  873. * call-like statements that
  874. * can be preceded by 'defer' and 'go'
  875. */
  876. pseudocall:
  877. pexpr '(' ')'
  878. {
  879. // $$ = nod(OCALL, $1, N);
  880. }
  881. | pexpr '(' expr_or_type_list ocomma ')'
  882. {
  883. // $$ = nod(OCALL, $1, N);
  884. // $$->list = $3;
  885. }
  886. | pexpr '(' expr_or_type_list LDDD ocomma ')'
  887. {
  888. // $$ = nod(OCALL, $1, N);
  889. // $$->list = $3;
  890. // $$->isddd = 1;
  891. }
  892. pexpr_no_paren:
  893. LLITERAL
  894. {
  895. // $$ = nodlit($1);
  896. }
  897. | name
  898. | pexpr '.' sym
  899. {
  900. // if($1->op == OPACK) {
  901. // Sym *s;
  902. // s = restrictlookup($3->name, $1->pkg);
  903. // $1->used = 1;
  904. // $$ = oldname(s);
  905. // break;
  906. // }
  907. // $$ = nod(OXDOT, $1, newname($3));
  908. }
  909. | pexpr '.' '(' expr_or_type ')'
  910. {
  911. // $$ = nod(ODOTTYPE, $1, $4);
  912. }
  913. | pexpr '.' '(' LTYPE ')'
  914. {
  915. // $$ = nod(OTYPESW, N, $1);
  916. }
  917. | pexpr '[' expr ']'
  918. {
  919. // $$ = nod(OINDEX, $1, $3);
  920. }
  921. | pexpr '[' oexpr ':' oexpr ']'
  922. {
  923. // $$ = nod(OSLICE, $1, nod(OKEY, $3, $5));
  924. }
  925. | pexpr '[' oexpr ':' oexpr ':' oexpr ']'
  926. {
  927. // if($5 == N)
  928. // yyerror("middle index required in 3-index slice");
  929. // if($7 == N)
  930. // yyerror("final index required in 3-index slice");
  931. // $$ = nod(OSLICE3, $1, nod(OKEY, $3, nod(OKEY, $5, $7)));
  932. }
  933. | pseudocall
  934. | convtype '(' expr ocomma ')'
  935. {
  936. // // conversion
  937. // $$ = nod(OCALL, $1, N);
  938. // $$->list = list1($3);
  939. }
  940. | comptype lbrace start_complit braced_keyval_list '}'
  941. {
  942. // $$ = $3;
  943. // $$->right = $1;
  944. // $$->list = $4;
  945. // fixlbrace($2);
  946. }
  947. | pexpr_no_paren '{' start_complit braced_keyval_list '}'
  948. {
  949. // $$ = $3;
  950. // $$->right = $1;
  951. // $$->list = $4;
  952. }
  953. | '(' expr_or_type ')' '{' start_complit braced_keyval_list '}'
  954. {
  955. // yyerror("cannot parenthesize type in composite literal");
  956. // $$ = $5;
  957. // $$->right = $2;
  958. // $$->list = $6;
  959. }
  960. | fnliteral
  961. start_complit:
  962. {
  963. // // composite expression.
  964. // // make node early so we get the right line number.
  965. // $$ = nod(OCOMPLIT, N, N);
  966. }
  967. keyval:
  968. expr ':' complitexpr
  969. {
  970. // $$ = nod(OKEY, $1, $3);
  971. }
  972. bare_complitexpr:
  973. expr
  974. {
  975. // // These nodes do not carry line numbers.
  976. // // Since a composite literal commonly spans several lines,
  977. // // the line number on errors may be misleading.
  978. // // Introduce a wrapper node to give the correct line.
  979. // $$ = $1;
  980. // switch($$->op) {
  981. // case ONAME:
  982. // case ONONAME:
  983. // case OTYPE:
  984. // case OPACK:
  985. // case OLITERAL:
  986. // $$ = nod(OPAREN, $$, N);
  987. // $$->implicit = 1;
  988. // }
  989. }
  990. | '{' start_complit braced_keyval_list '}'
  991. {
  992. // $$ = $2;
  993. // $$->list = $3;
  994. }
  995. complitexpr:
  996. expr
  997. | '{' start_complit braced_keyval_list '}'
  998. {
  999. // $$ = $2;
  1000. // $$->list = $3;
  1001. }
  1002. pexpr:
  1003. pexpr_no_paren
  1004. | '(' expr_or_type ')'
  1005. {
  1006. // $$ = $2;
  1007. //
  1008. // // Need to know on lhs of := whether there are ( ).
  1009. // // Don't bother with the OPAREN in other cases:
  1010. // // it's just a waste of memory and time.
  1011. // switch($$->op) {
  1012. // case ONAME:
  1013. // case ONONAME:
  1014. // case OPACK:
  1015. // case OTYPE:
  1016. // case OLITERAL:
  1017. // case OTYPESW:
  1018. // $$ = nod(OPAREN, $$, N);
  1019. // }
  1020. }
  1021. expr_or_type:
  1022. expr
  1023. | non_expr_type %prec PreferToRightParen
  1024. name_or_type:
  1025. ntype
  1026. lbrace:
  1027. LBODY
  1028. {
  1029. // $$ = LBODY;
  1030. }
  1031. | '{'
  1032. {
  1033. // $$ = '{';
  1034. }
  1035. /*
  1036. * names and types
  1037. * newname is used before declared
  1038. * oldname is used after declared
  1039. */
  1040. new_name:
  1041. sym
  1042. {
  1043. // if($1 == S)
  1044. // $$ = N;
  1045. // else
  1046. // $$ = newname($1);
  1047. }
  1048. dcl_name:
  1049. sym
  1050. {
  1051. // $$ = dclname($1);
  1052. }
  1053. onew_name:
  1054. {
  1055. // $$ = N;
  1056. }
  1057. | new_name
  1058. sym:
  1059. LNAME
  1060. {
  1061. // $$ = $1;
  1062. // // during imports, unqualified non-exported identifiers are from builtinpkg
  1063. // if(importpkg != nil && !exportname($1->name))
  1064. // $$ = pkglookup($1->name, builtinpkg);
  1065. }
  1066. | hidden_importsym
  1067. | '?'
  1068. {
  1069. // $$ = S;
  1070. }
  1071. hidden_importsym:
  1072. '@' LLITERAL '.' LNAME
  1073. {
  1074. // Pkg *p;
  1075. //
  1076. // if($2.u.sval->len == 0)
  1077. // p = importpkg;
  1078. // else {
  1079. // if(isbadimport($2.u.sval))
  1080. // errorexit();
  1081. // p = mkpkg($2.u.sval);
  1082. // }
  1083. // $$ = pkglookup($4->name, p);
  1084. }
  1085. | '@' LLITERAL '.' '?'
  1086. {
  1087. // Pkg *p;
  1088. //
  1089. // if($2.u.sval->len == 0)
  1090. // p = importpkg;
  1091. // else {
  1092. // if(isbadimport($2.u.sval))
  1093. // errorexit();
  1094. // p = mkpkg($2.u.sval);
  1095. // }
  1096. // $$ = pkglookup("?", p);
  1097. }
  1098. name:
  1099. sym %prec NotParen
  1100. {
  1101. // $$ = oldname($1);
  1102. // if($$->pack != N)
  1103. // $$->pack->used = 1;
  1104. }
  1105. labelname:
  1106. new_name
  1107. /*
  1108. * to avoid parsing conflicts, type is split into
  1109. * channel types
  1110. * function types
  1111. * parenthesized types
  1112. * any other type
  1113. * the type system makes additional restrictions,
  1114. * but those are not implemented in the grammar.
  1115. */
  1116. dotdotdot:
  1117. LDDD
  1118. {
  1119. // yyerror("final argument in variadic function missing type");
  1120. // $$ = nod(ODDD, typenod(typ(TINTER)), N);
  1121. }
  1122. | LDDD ntype
  1123. {
  1124. // $$ = nod(ODDD, $2, N);
  1125. }
  1126. ntype:
  1127. recvchantype
  1128. | fntype
  1129. | othertype
  1130. | ptrtype
  1131. | dotname
  1132. | '(' ntype ')'
  1133. {
  1134. // $$ = nod(OTPAREN, $2, N);
  1135. }
  1136. non_expr_type:
  1137. recvchantype
  1138. | fntype
  1139. | othertype
  1140. | '*' non_expr_type
  1141. {
  1142. // $$ = nod(OIND, $2, N);
  1143. }
  1144. non_recvchantype:
  1145. fntype
  1146. | othertype
  1147. | ptrtype
  1148. | dotname
  1149. | '(' ntype ')'
  1150. {
  1151. // $$ = nod(OTPAREN, $2, N);
  1152. }
  1153. convtype:
  1154. fntype
  1155. | othertype
  1156. comptype:
  1157. othertype
  1158. fnret_type:
  1159. recvchantype
  1160. | fntype
  1161. | othertype
  1162. | ptrtype
  1163. | dotname
  1164. dotname:
  1165. name
  1166. | name '.' sym
  1167. {
  1168. // if($1->op == OPACK) {
  1169. // Sym *s;
  1170. // s = restrictlookup($3->name, $1->pkg);
  1171. // $1->used = 1;
  1172. // $$ = oldname(s);
  1173. // break;
  1174. // }
  1175. // $$ = nod(OXDOT, $1, newname($3));
  1176. }
  1177. othertype:
  1178. '[' oexpr ']' ntype
  1179. {
  1180. // $$ = nod(OTARRAY, $2, $4);
  1181. }
  1182. | '[' LDDD ']' ntype
  1183. {
  1184. // // array literal of nelem
  1185. // $$ = nod(OTARRAY, nod(ODDD, N, N), $4);
  1186. }
  1187. | LCHAN non_recvchantype
  1188. {
  1189. // $$ = nod(OTCHAN, $2, N);
  1190. // $$->etype = Cboth;
  1191. }
  1192. | LCHAN LCOMM ntype
  1193. {
  1194. // $$ = nod(OTCHAN, $3, N);
  1195. // $$->etype = Csend;
  1196. }
  1197. | LMAP '[' ntype ']' ntype
  1198. {
  1199. // $$ = nod(OTMAP, $3, $5);
  1200. }
  1201. | structtype
  1202. | interfacetype
  1203. ptrtype:
  1204. '*' ntype
  1205. {
  1206. // $$ = nod(OIND, $2, N);
  1207. }
  1208. recvchantype:
  1209. LCOMM LCHAN ntype
  1210. {
  1211. // $$ = nod(OTCHAN, $3, N);
  1212. // $$->etype = Crecv;
  1213. }
  1214. structtype:
  1215. LSTRUCT lbrace structdcl_list osemi '}'
  1216. {
  1217. // $$ = nod(OTSTRUCT, N, N);
  1218. // $$->list = $3;
  1219. // fixlbrace($2);
  1220. }
  1221. | LSTRUCT lbrace '}'
  1222. {
  1223. // $$ = nod(OTSTRUCT, N, N);
  1224. // fixlbrace($2);
  1225. }
  1226. interfacetype:
  1227. LINTERFACE lbrace interfacedcl_list osemi '}'
  1228. {
  1229. // $$ = nod(OTINTER, N, N);
  1230. // $$->list = $3;
  1231. // fixlbrace($2);
  1232. }
  1233. | LINTERFACE lbrace '}'
  1234. {
  1235. // $$ = nod(OTINTER, N, N);
  1236. // fixlbrace($2);
  1237. }
  1238. /*
  1239. * function stuff
  1240. * all in one place to show how crappy it all is
  1241. */
  1242. xfndcl:
  1243. LFUNC fndcl fnbody
  1244. {
  1245. // $$ = $2;
  1246. // if($$ == N)
  1247. // break;
  1248. // if(noescape && $3 != nil)
  1249. // yyerror("can only use //go:noescape with external func implementations");
  1250. // $$->nbody = $3;
  1251. // $$->endlineno = lineno;
  1252. // $$->noescape = noescape;
  1253. // funcbody($$);
  1254. }
  1255. fndcl:
  1256. sym '(' oarg_type_list_ocomma ')' fnres
  1257. {
  1258. // Node *t;
  1259. //
  1260. // $$ = N;
  1261. // $3 = checkarglist($3, 1);
  1262. //
  1263. // if(strcmp($1->name, "init") == 0) {
  1264. // $1 = renameinit();
  1265. // if($3 != nil || $5 != nil)
  1266. // yyerror("func init must have no arguments and no return values");
  1267. // }
  1268. // if(strcmp(localpkg->name, "main") == 0 && strcmp($1->name, "main") == 0) {
  1269. // if($3 != nil || $5 != nil)
  1270. // yyerror("func main must have no arguments and no return values");
  1271. // }
  1272. //
  1273. // t = nod(OTFUNC, N, N);
  1274. // t->list = $3;
  1275. // t->rlist = $5;
  1276. //
  1277. // $$ = nod(ODCLFUNC, N, N);
  1278. // $$->nname = newname($1);
  1279. // $$->nname->defn = $$;
  1280. // $$->nname->ntype = t; // TODO: check if nname already has an ntype
  1281. // declare($$->nname, PFUNC);
  1282. //
  1283. // funchdr($$);
  1284. }
  1285. | '(' oarg_type_list_ocomma ')' sym '(' oarg_type_list_ocomma ')' fnres
  1286. {
  1287. // Node *rcvr, *t;
  1288. //
  1289. // $$ = N;
  1290. // $2 = checkarglist($2, 0);
  1291. // $6 = checkarglist($6, 1);
  1292. //
  1293. // if($2 == nil) {
  1294. // yyerror("method has no receiver");
  1295. // break;
  1296. // }
  1297. // if($2->next != nil) {
  1298. // yyerror("method has multiple receivers");
  1299. // break;
  1300. // }
  1301. // rcvr = $2->n;
  1302. // if(rcvr->op != ODCLFIELD) {
  1303. // yyerror("bad receiver in method");
  1304. // break;
  1305. // }
  1306. // if(rcvr->right->op == OTPAREN || (rcvr->right->op == OIND && rcvr->right->left->op == OTPAREN))
  1307. // yyerror("cannot parenthesize receiver type");
  1308. //
  1309. // t = nod(OTFUNC, rcvr, N);
  1310. // t->list = $6;
  1311. // t->rlist = $8;
  1312. //
  1313. // $$ = nod(ODCLFUNC, N, N);
  1314. // $$->shortname = newname($4);
  1315. // $$->nname = methodname1($$->shortname, rcvr->right);
  1316. // $$->nname->defn = $$;
  1317. // $$->nname->ntype = t;
  1318. // $$->nname->nointerface = nointerface;
  1319. // declare($$->nname, PFUNC);
  1320. //
  1321. // funchdr($$);
  1322. }
  1323. hidden_fndcl:
  1324. hidden_pkg_importsym '(' ohidden_funarg_list ')' ohidden_funres
  1325. {
  1326. // Sym *s;
  1327. // Type *t;
  1328. //
  1329. // $$ = N;
  1330. //
  1331. // s = $1;
  1332. // t = functype(N, $3, $5);
  1333. //
  1334. // importsym(s, ONAME);
  1335. // if(s->def != N && s->def->op == ONAME) {
  1336. // if(eqtype(t, s->def->type)) {
  1337. // dclcontext = PDISCARD; // since we skip funchdr below
  1338. // break;
  1339. // }
  1340. // yyerror("inconsistent definition for func %S during import\n\t%T\n\t%T", s, s->def->type, t);
  1341. // }
  1342. //
  1343. // $$ = newname(s);
  1344. // $$->type = t;
  1345. // declare($$, PFUNC);
  1346. //
  1347. // funchdr($$);
  1348. }
  1349. | '(' hidden_funarg_list ')' sym '(' ohidden_funarg_list ')' ohidden_funres
  1350. {
  1351. // $$ = methodname1(newname($4), $2->n->right);
  1352. // $$->type = functype($2->n, $6, $8);
  1353. //
  1354. // checkwidth($$->type);
  1355. // addmethod($4, $$->type, 0, nointerface);
  1356. // nointerface = 0;
  1357. // funchdr($$);
  1358. //
  1359. // // inl.c's inlnode in on a dotmeth node expects to find the inlineable body as
  1360. // // (dotmeth's type)->nname->inl, and dotmeth's type has been pulled
  1361. // // out by typecheck's lookdot as this $$->ttype. So by providing
  1362. // // this back link here we avoid special casing there.
  1363. // $$->type->nname = $$;
  1364. }
  1365. fntype:
  1366. LFUNC '(' oarg_type_list_ocomma ')' fnres
  1367. {
  1368. // $3 = checkarglist($3, 1);
  1369. // $$ = nod(OTFUNC, N, N);
  1370. // $$->list = $3;
  1371. // $$->rlist = $5;
  1372. }
  1373. fnbody:
  1374. {
  1375. // $$ = nil;
  1376. }
  1377. | '{' stmt_list '}'
  1378. {
  1379. // $$ = $2;
  1380. // if($$ == nil)
  1381. // $$ = list1(nod(OEMPTY, N, N));
  1382. }
  1383. fnres:
  1384. %prec NotParen
  1385. {
  1386. // $$ = nil;
  1387. }
  1388. | fnret_type
  1389. {
  1390. // $$ = list1(nod(ODCLFIELD, N, $1));
  1391. }
  1392. | '(' oarg_type_list_ocomma ')'
  1393. {
  1394. // $2 = checkarglist($2, 0);
  1395. // $$ = $2;
  1396. }
  1397. fnlitdcl:
  1398. fntype
  1399. {
  1400. // closurehdr($1);
  1401. }
  1402. fnliteral:
  1403. fnlitdcl lbrace stmt_list '}'
  1404. {
  1405. // $$ = closurebody($3);
  1406. // fixlbrace($2);
  1407. }
  1408. | fnlitdcl error
  1409. {
  1410. // $$ = closurebody(nil);
  1411. }
  1412. /*
  1413. * lists of things
  1414. * note that they are left recursive
  1415. * to conserve yacc stack. they need to
  1416. * be reversed to interpret correctly
  1417. */
  1418. xdcl_list:
  1419. {
  1420. // $$ = nil;
  1421. }
  1422. | xdcl_list xdcl ';'
  1423. {
  1424. // $$ = concat($1, $2);
  1425. // if(nsyntaxerrors == 0)
  1426. // testdclstack();
  1427. // nointerface = 0;
  1428. // noescape = 0;
  1429. }
  1430. vardcl_list:
  1431. vardcl
  1432. | vardcl_list ';' vardcl
  1433. {
  1434. // $$ = concat($1, $3);
  1435. }
  1436. constdcl_list:
  1437. constdcl1
  1438. | constdcl_list ';' constdcl1
  1439. {
  1440. // $$ = concat($1, $3);
  1441. }
  1442. typedcl_list:
  1443. typedcl
  1444. {
  1445. // $$ = list1($1);
  1446. }
  1447. | typedcl_list ';' typedcl
  1448. {
  1449. // $$ = list($1, $3);
  1450. }
  1451. structdcl_list:
  1452. structdcl
  1453. | structdcl_list ';' structdcl
  1454. {
  1455. // $$ = concat($1, $3);
  1456. }
  1457. interfacedcl_list:
  1458. interfacedcl
  1459. {
  1460. // $$ = list1($1);
  1461. }
  1462. | interfacedcl_list ';' interfacedcl
  1463. {
  1464. // $$ = list($1, $3);
  1465. }
  1466. structdcl:
  1467. new_name_list ntype oliteral
  1468. {
  1469. // NodeList *l;
  1470. //
  1471. // Node *n;
  1472. // l = $1;
  1473. // if(l == nil) {
  1474. // // ? symbol, during import (list1(N) == nil)
  1475. // n = $2;
  1476. // if(n->op == OIND)
  1477. // n = n->left;
  1478. // n = embedded(n->sym, importpkg);
  1479. // n->right = $2;
  1480. // n->val = $3;
  1481. // $$ = list1(n);
  1482. // break;
  1483. // }
  1484. //
  1485. // for(l=$1; l; l=l->next) {
  1486. // l->n = nod(ODCLFIELD, l->n, $2);
  1487. // l->n->val = $3;
  1488. // }
  1489. }
  1490. | embed oliteral
  1491. {
  1492. // $1->val = $2;
  1493. // $$ = list1($1);
  1494. }
  1495. | '(' embed ')' oliteral
  1496. {
  1497. // $2->val = $4;
  1498. // $$ = list1($2);
  1499. // yyerror("cannot parenthesize embedded type");
  1500. }
  1501. | '*' embed oliteral
  1502. {
  1503. // $2->right = nod(OIND, $2->right, N);
  1504. // $2->val = $3;
  1505. // $$ = list1($2);
  1506. }
  1507. | '(' '*' embed ')' oliteral
  1508. {
  1509. // $3->right = nod(OIND, $3->right, N);
  1510. // $3->val = $5;
  1511. // $$ = list1($3);
  1512. // yyerror("cannot parenthesize embedded type");
  1513. }
  1514. | '*' '(' embed ')' oliteral
  1515. {
  1516. // $3->right = nod(OIND, $3->right, N);
  1517. // $3->val = $5;
  1518. // $$ = list1($3);
  1519. // yyerror("cannot parenthesize embedded type");
  1520. }
  1521. packname:
  1522. LNAME
  1523. {
  1524. // Node *n;
  1525. //
  1526. // $$ = $1;
  1527. // n = oldname($1);
  1528. // if(n->pack != N)
  1529. // n->pack->used = 1;
  1530. }
  1531. | LNAME '.' sym
  1532. {
  1533. // Pkg *pkg;
  1534. //
  1535. // if($1->def == N || $1->def->op != OPACK) {
  1536. // yyerror("%S is not a package", $1);
  1537. // pkg = localpkg;
  1538. // } else {
  1539. // $1->def->used = 1;
  1540. // pkg = $1->def->pkg;
  1541. // }
  1542. // $$ = restrictlookup($3->name, pkg);
  1543. }
  1544. embed:
  1545. packname
  1546. {
  1547. // $$ = embedded($1, localpkg);
  1548. }
  1549. interfacedcl:
  1550. new_name indcl
  1551. {
  1552. // $$ = nod(ODCLFIELD, $1, $2);
  1553. // ifacedcl($$);
  1554. }
  1555. | packname
  1556. {
  1557. // $$ = nod(ODCLFIELD, N, oldname($1));
  1558. }
  1559. | '(' packname ')'
  1560. {
  1561. // $$ = nod(ODCLFIELD, N, oldname($2));
  1562. // yyerror("cannot parenthesize embedded type");
  1563. }
  1564. indcl:
  1565. '(' oarg_type_list_ocomma ')' fnres
  1566. {
  1567. // // without func keyword
  1568. // $2 = checkarglist($2, 1);
  1569. // $$ = nod(OTFUNC, fakethis(), N);
  1570. // $$->list = $2;
  1571. // $$->rlist = $4;
  1572. }
  1573. /*
  1574. * function arguments.
  1575. */
  1576. arg_type:
  1577. name_or_type
  1578. | sym name_or_type
  1579. {
  1580. // $$ = nod(ONONAME, N, N);
  1581. // $$->sym = $1;
  1582. // $$ = nod(OKEY, $$, $2);
  1583. }
  1584. | sym dotdotdot
  1585. {
  1586. // $$ = nod(ONONAME, N, N);
  1587. // $$->sym = $1;
  1588. // $$ = nod(OKEY, $$, $2);
  1589. }
  1590. | dotdotdot
  1591. arg_type_list:
  1592. arg_type
  1593. {
  1594. // $$ = list1($1);
  1595. }
  1596. | arg_type_list ',' arg_type
  1597. {
  1598. // $$ = list($1, $3);
  1599. }
  1600. oarg_type_list_ocomma:
  1601. {
  1602. // $$ = nil;
  1603. }
  1604. | arg_type_list ocomma
  1605. {
  1606. // $$ = $1;
  1607. }
  1608. /*
  1609. * statement
  1610. */
  1611. stmt:
  1612. {
  1613. // $$ = N;
  1614. }
  1615. | compound_stmt
  1616. | common_dcl
  1617. {
  1618. // $$ = liststmt($1);
  1619. }
  1620. | non_dcl_stmt
  1621. | error
  1622. {
  1623. // $$ = N;
  1624. }
  1625. non_dcl_stmt:
  1626. simple_stmt
  1627. | for_stmt
  1628. | switch_stmt
  1629. | select_stmt
  1630. | if_stmt
  1631. | labelname ':'
  1632. {
  1633. // $1 = nod(OLABEL, $1, N);
  1634. // $1->sym = dclstack; // context, for goto restrictions
  1635. }
  1636. stmt
  1637. {
  1638. // NodeList *l;
  1639. //
  1640. // $1->defn = $4;
  1641. // l = list1($1);
  1642. // if($4)
  1643. // l = list(l, $4);
  1644. // $$ = liststmt(l);
  1645. }
  1646. | LFALL
  1647. {
  1648. // // will be converted to OFALL
  1649. // $$ = nod(OXFALL, N, N);
  1650. // $$->xoffset = block;
  1651. }
  1652. | LBREAK onew_name
  1653. {
  1654. // $$ = nod(OBREAK, $2, N);
  1655. }
  1656. | LCONTINUE onew_name
  1657. {
  1658. // $$ = nod(OCONTINUE, $2, N);
  1659. }
  1660. | LGO pseudocall
  1661. {
  1662. // $$ = nod(OPROC, $2, N);
  1663. }
  1664. | LDEFER pseudocall
  1665. {
  1666. // $$ = nod(ODEFER, $2, N);
  1667. }
  1668. | LGOTO new_name
  1669. {
  1670. // $$ = nod(OGOTO, $2, N);
  1671. // $$->sym = dclstack; // context, for goto restrictions
  1672. }
  1673. | LRETURN oexpr_list
  1674. {
  1675. // $$ = nod(ORETURN, N, N);
  1676. // $$->list = $2;
  1677. // if($$->list == nil && curfn != N) {
  1678. // NodeList *l;
  1679. //
  1680. // for(l=curfn->dcl; l; l=l->next) {
  1681. // if(l->n->class == PPARAM)
  1682. // continue;
  1683. // if(l->n->class != PPARAMOUT)
  1684. // break;
  1685. // if(l->n->sym->def != l->n)
  1686. // yyerror("%s is shadowed during return", l->n->sym->name);
  1687. // }
  1688. // }
  1689. }
  1690. stmt_list:
  1691. stmt
  1692. {
  1693. // $$ = nil;
  1694. // if($1 != N)
  1695. // $$ = list1($1);
  1696. }
  1697. | stmt_list ';' stmt
  1698. {
  1699. // $$ = $1;
  1700. // if($3 != N)
  1701. // $$ = list($$, $3);
  1702. }
  1703. new_name_list:
  1704. new_name
  1705. {
  1706. // $$ = list1($1);
  1707. }
  1708. | new_name_list ',' new_name
  1709. {
  1710. // $$ = list($1, $3);
  1711. }
  1712. dcl_name_list:
  1713. dcl_name
  1714. {
  1715. // $$ = list1($1);
  1716. }
  1717. | dcl_name_list ',' dcl_name
  1718. {
  1719. // $$ = list($1, $3);
  1720. }
  1721. expr_list:
  1722. expr
  1723. {
  1724. // $$ = list1($1);
  1725. }
  1726. | expr_list ',' expr
  1727. {
  1728. // $$ = list($1, $3);
  1729. }
  1730. expr_or_type_list:
  1731. expr_or_type
  1732. {
  1733. // $$ = list1($1);
  1734. }
  1735. | expr_or_type_list ',' expr_or_type
  1736. {
  1737. // $$ = list($1, $3);
  1738. }
  1739. /*
  1740. * list of combo of keyval and val
  1741. */
  1742. keyval_list:
  1743. keyval
  1744. {
  1745. // $$ = list1($1);
  1746. }
  1747. | bare_complitexpr
  1748. {
  1749. // $$ = list1($1);
  1750. }
  1751. | keyval_list ',' keyval
  1752. {
  1753. // $$ = list($1, $3);
  1754. }
  1755. | keyval_list ',' bare_complitexpr
  1756. {
  1757. // $$ = list($1, $3);
  1758. }
  1759. braced_keyval_list:
  1760. {
  1761. // $$ = nil;
  1762. }
  1763. | keyval_list ocomma
  1764. {
  1765. // $$ = $1;
  1766. }
  1767. /*
  1768. * optional things
  1769. */
  1770. osemi:
  1771. | ';'
  1772. ocomma:
  1773. | ','
  1774. oexpr:
  1775. {
  1776. // $$ = N;
  1777. }
  1778. | expr
  1779. oexpr_list:
  1780. {
  1781. // $$ = nil;
  1782. }
  1783. | expr_list
  1784. osimple_stmt:
  1785. {
  1786. // $$ = N;
  1787. }
  1788. | simple_stmt
  1789. ohidden_funarg_list:
  1790. {
  1791. // $$ = nil;
  1792. }
  1793. | hidden_funarg_list
  1794. ohidden_structdcl_list:
  1795. {
  1796. // $$ = nil;
  1797. }
  1798. | hidden_structdcl_list
  1799. ohidden_interfacedcl_list:
  1800. {
  1801. // $$ = nil;
  1802. }
  1803. | hidden_interfacedcl_list
  1804. oliteral:
  1805. {
  1806. // $$.ctype = CTxxx;
  1807. }
  1808. | LLITERAL
  1809. /*
  1810. * import syntax from package header
  1811. */
  1812. hidden_import:
  1813. LIMPORT LNAME LLITERAL ';'
  1814. {
  1815. // importimport($2, $3.u.sval);
  1816. }
  1817. | LVAR hidden_pkg_importsym hidden_type ';'
  1818. {
  1819. // importvar($2, $3);
  1820. }
  1821. | LCONST hidden_pkg_importsym '=' hidden_constant ';'
  1822. {
  1823. // importconst($2, types[TIDEAL], $4);
  1824. }
  1825. | LCONST hidden_pkg_importsym hidden_type '=' hidden_constant ';'
  1826. {
  1827. // importconst($2, $3, $5);
  1828. }
  1829. | LTYPE hidden_pkgtype hidden_type ';'
  1830. {
  1831. // importtype($2, $3);
  1832. }
  1833. | LFUNC hidden_fndcl fnbody ';'
  1834. {
  1835. // if($2 == N) {
  1836. // dclcontext = PEXTERN; // since we skip the funcbody below
  1837. // break;
  1838. // }
  1839. //
  1840. // $2->inl = $3;
  1841. //
  1842. // funcbody($2);
  1843. // importlist = list(importlist, $2);
  1844. //
  1845. // if(debug['E']) {
  1846. // print("import [%Z] func %lN \n", importpkg->path, $2);
  1847. // if(debug['m'] > 2 && $2->inl)
  1848. // print("inl body:%+H\n", $2->inl);
  1849. // }
  1850. }
  1851. hidden_pkg_importsym:
  1852. hidden_importsym
  1853. {
  1854. // $$ = $1;
  1855. // structpkg = $$->pkg;
  1856. }
  1857. hidden_pkgtype:
  1858. hidden_pkg_importsym
  1859. {
  1860. // $$ = pkgtype($1);
  1861. // importsym($1, OTYPE);
  1862. }
  1863. /*
  1864. * importing types
  1865. */
  1866. hidden_type:
  1867. hidden_type_misc
  1868. | hidden_type_recv_chan
  1869. | hidden_type_func
  1870. hidden_type_non_recv_chan:
  1871. hidden_type_misc
  1872. | hidden_type_func
  1873. hidden_type_misc:
  1874. hidden_importsym
  1875. {
  1876. // $$ = pkgtype($1);
  1877. }
  1878. | LNAME
  1879. {
  1880. // // predefined name like uint8
  1881. // $1 = pkglookup($1->name, builtinpkg);
  1882. // if($1->def == N || $1->def->op != OTYPE) {
  1883. // yyerror("%s is not a type", $1->name);
  1884. // $$ = T;
  1885. // } else
  1886. // $$ = $1->def->type;
  1887. }
  1888. | '[' ']' hidden_type
  1889. {
  1890. // $$ = aindex(N, $3);
  1891. }
  1892. | '[' LLITERAL ']' hidden_type
  1893. {
  1894. // $$ = aindex(nodlit($2), $4);
  1895. }
  1896. | LMAP '[' hidden_type ']' hidden_type
  1897. {
  1898. // $$ = maptype($3, $5);
  1899. }
  1900. | LSTRUCT '{' ohidden_structdcl_list '}'
  1901. {
  1902. // $$ = tostruct($3);
  1903. }
  1904. | LINTERFACE '{' ohidden_interfacedcl_list '}'
  1905. {
  1906. // $$ = tointerface($3);
  1907. }
  1908. | '*' hidden_type
  1909. {
  1910. // $$ = ptrto($2);
  1911. }
  1912. | LCHAN hidden_type_non_recv_chan
  1913. {
  1914. // $$ = typ(TCHAN);
  1915. // $$->type = $2;
  1916. // $$->chan = Cboth;
  1917. }
  1918. | LCHAN '(' hidden_type_recv_chan ')'
  1919. {
  1920. // $$ = typ(TCHAN);
  1921. // $$->type = $3;
  1922. // $$->chan = Cboth;
  1923. }
  1924. | LCHAN LCOMM hidden_type
  1925. {
  1926. // $$ = typ(TCHAN);
  1927. // $$->type = $3;
  1928. // $$->chan = Csend;
  1929. }
  1930. hidden_type_recv_chan:
  1931. LCOMM LCHAN hidden_type
  1932. {
  1933. // $$ = typ(TCHAN);
  1934. // $$->type = $3;
  1935. // $$->chan = Crecv;
  1936. }
  1937. hidden_type_func:
  1938. LFUNC '(' ohidden_funarg_list ')' ohidden_funres
  1939. {
  1940. // $$ = functype(nil, $3, $5);
  1941. }
  1942. hidden_funarg:
  1943. sym hidden_type oliteral
  1944. {
  1945. // $$ = nod(ODCLFIELD, N, typenod($2));
  1946. // if($1)
  1947. // $$->left = newname($1);
  1948. // $$->val = $3;
  1949. }
  1950. | sym LDDD hidden_type oliteral
  1951. {
  1952. // Type *t;
  1953. //
  1954. // t = typ(TARRAY);
  1955. // t->bound = -1;
  1956. // t->type = $3;
  1957. //
  1958. // $$ = nod(ODCLFIELD, N, typenod(t));
  1959. // if($1)
  1960. // $$->left = newname($1);
  1961. // $$->isddd = 1;
  1962. // $$->val = $4;
  1963. }
  1964. hidden_structdcl:
  1965. sym hidden_type oliteral
  1966. {
  1967. // Sym *s;
  1968. // Pkg *p;
  1969. //
  1970. // if($1 != S && strcmp($1->name, "?") != 0) {
  1971. // $$ = nod(ODCLFIELD, newname($1), typenod($2));
  1972. // $$->val = $3;
  1973. // } else {
  1974. // s = $2->sym;
  1975. // if(s == S && isptr[$2->etype])
  1976. // s = $2->type->sym;
  1977. // p = importpkg;
  1978. // if($1 != S)
  1979. // p = $1->pkg;
  1980. // $$ = embedded(s, p);
  1981. // $$->right = typenod($2);
  1982. // $$->val = $3;
  1983. // }
  1984. }
  1985. hidden_interfacedcl:
  1986. sym '(' ohidden_funarg_list ')' ohidden_funres
  1987. {
  1988. // $$ = nod(ODCLFIELD, newname($1), typenod(functype(fakethis(), $3, $5)));
  1989. }
  1990. | hidden_type
  1991. {
  1992. // $$ = nod(ODCLFIELD, N, typenod($1));
  1993. }
  1994. ohidden_funres:
  1995. {
  1996. // $$ = nil;
  1997. }
  1998. | hidden_funres
  1999. hidden_funres:
  2000. '(' ohidden_funarg_list ')'
  2001. {
  2002. // $$ = $2;
  2003. }
  2004. | hidden_type
  2005. {
  2006. // $$ = list1(nod(ODCLFIELD, N, typenod($1)));
  2007. }
  2008. /*
  2009. * importing constants
  2010. */
  2011. hidden_literal:
  2012. LLITERAL
  2013. {
  2014. // $$ = nodlit($1);
  2015. }
  2016. | '-' LLITERAL
  2017. {
  2018. // $$ = nodlit($2);
  2019. // switch($$->val.ctype){
  2020. // case CTINT:
  2021. // case CTRUNE:
  2022. // mpnegfix($$->val.u.xval);
  2023. // break;
  2024. // case CTFLT:
  2025. // mpnegflt($$->val.u.fval);
  2026. // break;
  2027. // case CTCPLX:
  2028. // mpnegflt(&$$->val.u.cval->real);
  2029. // mpnegflt(&$$->val.u.cval->imag);
  2030. // break;
  2031. // default:
  2032. // yyerror("bad negated constant");
  2033. // }
  2034. }
  2035. | sym
  2036. {
  2037. // $$ = oldname(pkglookup($1->name, builtinpkg));
  2038. // if($$->op != OLITERAL)
  2039. // yyerror("bad constant %S", $$->sym);
  2040. }
  2041. hidden_constant:
  2042. hidden_literal
  2043. | '(' hidden_literal '+' hidden_literal ')'
  2044. {
  2045. // if($2->val.ctype == CTRUNE && $4->val.ctype == CTINT) {
  2046. // $$ = $2;
  2047. // mpaddfixfix($2->val.u.xval, $4->val.u.xval, 0);
  2048. // break;
  2049. // }
  2050. // $4->val.u.cval->real = $4->val.u.cval->imag;
  2051. // mpmovecflt(&$4->val.u.cval->imag, 0.0);
  2052. // $$ = nodcplxlit($2->val, $4->val);
  2053. }
  2054. hidden_import_list:
  2055. | hidden_import_list hidden_import
  2056. hidden_funarg_list:
  2057. hidden_funarg
  2058. {
  2059. // $$ = list1($1);
  2060. }
  2061. | hidden_funarg_list ',' hidden_funarg
  2062. {
  2063. // $$ = list($1, $3);
  2064. }
  2065. hidden_structdcl_list:
  2066. hidden_structdcl
  2067. {
  2068. // $$ = list1($1);
  2069. }
  2070. | hidden_structdcl_list ';' hidden_structdcl
  2071. {
  2072. // $$ = list($1, $3);
  2073. }
  2074. hidden_interfacedcl_list:
  2075. hidden_interfacedcl
  2076. {
  2077. // $$ = list1($1);
  2078. }
  2079. | hidden_interfacedcl_list ';' hidden_interfacedcl
  2080. {
  2081. // $$ = list($1, $3);
  2082. }
  2083. %%
  2084. func fixlbrace(lbr int) {
  2085. // If the opening brace was an LBODY,
  2086. // set up for another one now that we're done.
  2087. // See comment in lex.c about loophack.
  2088. // if lbr == LBODY {
  2089. // loophack = 1
  2090. // }
  2091. }