expr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <ctype.h>
  13. #include <mach.h>
  14. #define Extern extern
  15. #include "acid.h"
  16. static int fsize[] =
  17. {
  18. ['A'] 4,
  19. ['B'] 4,
  20. ['C'] 1,
  21. ['D'] 4,
  22. ['F'] 8,
  23. ['G'] 8,
  24. ['O'] 4,
  25. ['Q'] 4,
  26. ['R'] 4,
  27. ['S'] 4,
  28. ['U'] 4,
  29. ['V'] 8,
  30. ['W'] 8,
  31. ['X'] 4,
  32. ['Y'] 8,
  33. ['Z'] 8,
  34. ['a'] 4,
  35. ['b'] 1,
  36. ['c'] 1,
  37. ['d'] 2,
  38. ['f'] 4,
  39. ['g'] 4,
  40. ['o'] 2,
  41. ['q'] 2,
  42. ['r'] 2,
  43. ['s'] 4,
  44. ['u'] 2,
  45. ['x'] 2,
  46. ['3'] 10,
  47. ['8'] 10,
  48. };
  49. int
  50. fmtsize(Value *v)
  51. {
  52. int ret;
  53. switch(v->fmt) {
  54. default:
  55. return fsize[v->fmt];
  56. case 'i':
  57. case 'I':
  58. if(v->type != TINT || machdata == 0)
  59. error("no size for i fmt pointer ++/--");
  60. ret = (*machdata->instsize)(cormap, v->ival);
  61. if(ret < 0) {
  62. ret = (*machdata->instsize)(symmap, v->ival);
  63. if(ret < 0)
  64. error("%r");
  65. }
  66. return ret;
  67. }
  68. }
  69. void
  70. chklval(Node *lp)
  71. {
  72. if(lp->op != ONAME)
  73. error("need l-value");
  74. }
  75. void
  76. olist(Node *n, Node *res)
  77. {
  78. expr(n->left, res);
  79. expr(n->right, res);
  80. }
  81. void
  82. oeval(Node *n, Node *res)
  83. {
  84. expr(n->left, res);
  85. if(res->type != TCODE)
  86. error("bad type for eval");
  87. expr(res->cc, res);
  88. }
  89. void
  90. ocast(Node *n, Node *res)
  91. {
  92. if(n->sym->lt == 0)
  93. error("%s is not a complex type", n->sym->name);
  94. expr(n->left, res);
  95. res->comt = n->sym->lt;
  96. res->fmt = 'a';
  97. }
  98. void
  99. oindm(Node *n, Node *res)
  100. {
  101. Map *m;
  102. Node l;
  103. m = cormap;
  104. if(m == 0)
  105. m = symmap;
  106. expr(n->left, &l);
  107. if(l.type != TINT)
  108. error("bad type for *");
  109. if(m == 0)
  110. error("no map for *");
  111. indir(m, l.ival, l.fmt, res);
  112. res->comt = l.comt;
  113. }
  114. void
  115. oindc(Node *n, Node *res)
  116. {
  117. Map *m;
  118. Node l;
  119. m = symmap;
  120. if(m == 0)
  121. m = cormap;
  122. expr(n->left, &l);
  123. if(l.type != TINT)
  124. error("bad type for @");
  125. if(m == 0)
  126. error("no map for @");
  127. indir(m, l.ival, l.fmt, res);
  128. res->comt = l.comt;
  129. }
  130. void
  131. oframe(Node *n, Node *res)
  132. {
  133. char *p;
  134. Node *lp;
  135. uint64_t ival;
  136. Frtype *f;
  137. p = n->sym->name;
  138. while(*p && *p == '$')
  139. p++;
  140. lp = n->left;
  141. if(localaddr(cormap, p, lp->sym->name, &ival, rget) < 0)
  142. error("colon: %r");
  143. res->ival = ival;
  144. res->op = OCONST;
  145. res->fmt = 'X';
  146. res->type = TINT;
  147. /* Try and set comt */
  148. for(f = n->sym->local; f; f = f->next) {
  149. if(f->var == lp->sym) {
  150. res->comt = f->type;
  151. res->fmt = 'a';
  152. break;
  153. }
  154. }
  155. }
  156. void
  157. oindex(Node *n, Node *res)
  158. {
  159. Node l, r;
  160. expr(n->left, &l);
  161. expr(n->right, &r);
  162. if(r.type != TINT)
  163. error("bad type for []");
  164. switch(l.type) {
  165. default:
  166. error("lhs[] has bad type");
  167. case TINT:
  168. indir(cormap, l.ival+(r.ival*fsize[l.fmt]), l.fmt, res);
  169. res->comt = l.comt;
  170. res->fmt = l.fmt;
  171. break;
  172. case TLIST:
  173. nthelem(l.l, r.ival, res);
  174. break;
  175. case TSTRING:
  176. res->ival = 0;
  177. if(r.ival >= 0 && r.ival < l.string->len) {
  178. int xx8; /* to get around bug in vc */
  179. xx8 = r.ival;
  180. res->ival = l.string->string[xx8];
  181. }
  182. res->op = OCONST;
  183. res->type = TINT;
  184. res->fmt = 'c';
  185. break;
  186. }
  187. }
  188. void
  189. oappend(Node *n, Node *res)
  190. {
  191. Value *v;
  192. Node r, l;
  193. int empty;
  194. expr(n->left, &l);
  195. expr(n->right, &r);
  196. if(l.type != TLIST)
  197. error("must append to list");
  198. empty = (l.l == nil && (n->left->op == ONAME));
  199. append(res, &l, &r);
  200. if(empty) {
  201. v = n->left->sym->v;
  202. v->type = res->type;
  203. v->Store = res->Store;
  204. v->comt = res->comt;
  205. }
  206. }
  207. void
  208. odelete(Node *n, Node *res)
  209. {
  210. Node l, r;
  211. expr(n->left, &l);
  212. expr(n->right, &r);
  213. if(l.type != TLIST)
  214. error("must delete from list");
  215. if(r.type != TINT)
  216. error("delete index must be integer");
  217. delete(l.l, r.ival, res);
  218. }
  219. void
  220. ohead(Node *n, Node *res)
  221. {
  222. Node l;
  223. expr(n->left, &l);
  224. if(l.type != TLIST)
  225. error("head needs list");
  226. res->op = OCONST;
  227. if(l.l) {
  228. res->type = l.l->type;
  229. res->Store = l.l->Store;
  230. }
  231. else {
  232. res->type = TLIST;
  233. res->l = 0;
  234. }
  235. }
  236. void
  237. otail(Node *n, Node *res)
  238. {
  239. Node l;
  240. expr(n->left, &l);
  241. if(l.type != TLIST)
  242. error("tail needs list");
  243. res->op = OCONST;
  244. res->type = TLIST;
  245. if(l.l)
  246. res->l = l.l->next;
  247. else
  248. res->l = 0;
  249. }
  250. void
  251. oconst(Node *n, Node *res)
  252. {
  253. res->op = OCONST;
  254. res->type = n->type;
  255. res->Store = n->Store;
  256. res->comt = n->comt;
  257. }
  258. void
  259. oname(Node *n, Node *res)
  260. {
  261. Value *v;
  262. v = n->sym->v;
  263. if(v->set == 0)
  264. error("%s used but not set", n->sym->name);
  265. res->op = OCONST;
  266. res->type = v->type;
  267. res->Store = v->Store;
  268. res->comt = v->comt;
  269. }
  270. void
  271. octruct(Node *n, Node *res)
  272. {
  273. res->op = OCONST;
  274. res->type = TLIST;
  275. res->l = construct(n->left);
  276. }
  277. void
  278. oasgn(Node *n, Node *res)
  279. {
  280. Node *lp, r;
  281. Value *v;
  282. lp = n->left;
  283. switch(lp->op) {
  284. case OINDM:
  285. windir(cormap, lp->left, n->right, res);
  286. break;
  287. case OINDC:
  288. windir(symmap, lp->left, n->right, res);
  289. break;
  290. default:
  291. chklval(lp);
  292. v = lp->sym->v;
  293. expr(n->right, &r);
  294. v->set = 1;
  295. v->type = r.type;
  296. v->Store = r.Store;
  297. res->op = OCONST;
  298. res->type = v->type;
  299. res->Store = v->Store;
  300. res->comt = v->comt;
  301. }
  302. }
  303. void
  304. oadd(Node *n, Node *res)
  305. {
  306. Node l, r;
  307. if(n->right == nil){ /* unary + */
  308. expr(n->left, res);
  309. return;
  310. }
  311. expr(n->left, &l);
  312. expr(n->right, &r);
  313. res->fmt = l.fmt;
  314. res->op = OCONST;
  315. res->type = TFLOAT;
  316. switch(l.type) {
  317. default:
  318. error("bad lhs type +");
  319. case TINT:
  320. switch(r.type) {
  321. case TINT:
  322. res->type = TINT;
  323. res->ival = l.ival+r.ival;
  324. break;
  325. case TFLOAT:
  326. res->fval = l.ival+r.fval;
  327. break;
  328. default:
  329. error("bad rhs type +");
  330. }
  331. break;
  332. case TFLOAT:
  333. switch(r.type) {
  334. case TINT:
  335. res->fval = l.fval+r.ival;
  336. break;
  337. case TFLOAT:
  338. res->fval = l.fval+r.fval;
  339. break;
  340. default:
  341. error("bad rhs type +");
  342. }
  343. break;
  344. case TSTRING:
  345. if(r.type == TSTRING) {
  346. res->type = TSTRING;
  347. res->fmt = 's';
  348. res->string = stradd(l.string, r.string);
  349. break;
  350. }
  351. if(r.type == TINT) {
  352. res->type = TSTRING;
  353. res->fmt = 's';
  354. res->string = straddrune(l.string, r.ival);
  355. break;
  356. }
  357. error("bad rhs for +");
  358. case TLIST:
  359. res->type = TLIST;
  360. switch(r.type) {
  361. case TLIST:
  362. res->l = addlist(l.l, r.l);
  363. break;
  364. default:
  365. r.left = 0;
  366. r.right = 0;
  367. res->l = addlist(l.l, construct(&r));
  368. break;
  369. }
  370. }
  371. }
  372. void
  373. osub(Node *n, Node *res)
  374. {
  375. Node l, r;
  376. expr(n->left, &l);
  377. expr(n->right, &r);
  378. res->fmt = l.fmt;
  379. res->op = OCONST;
  380. res->type = TFLOAT;
  381. switch(l.type) {
  382. default:
  383. error("bad lhs type -");
  384. case TINT:
  385. switch(r.type) {
  386. case TINT:
  387. res->type = TINT;
  388. res->ival = l.ival-r.ival;
  389. break;
  390. case TFLOAT:
  391. res->fval = l.ival-r.fval;
  392. break;
  393. default:
  394. error("bad rhs type -");
  395. }
  396. break;
  397. case TFLOAT:
  398. switch(r.type) {
  399. case TINT:
  400. res->fval = l.fval-r.ival;
  401. break;
  402. case TFLOAT:
  403. res->fval = l.fval-r.fval;
  404. break;
  405. default:
  406. error("bad rhs type -");
  407. }
  408. break;
  409. }
  410. }
  411. void
  412. omul(Node *n, Node *res)
  413. {
  414. Node l, r;
  415. expr(n->left, &l);
  416. expr(n->right, &r);
  417. res->fmt = l.fmt;
  418. res->op = OCONST;
  419. res->type = TFLOAT;
  420. switch(l.type) {
  421. default:
  422. error("bad lhs type *");
  423. case TINT:
  424. switch(r.type) {
  425. case TINT:
  426. res->type = TINT;
  427. res->ival = l.ival*r.ival;
  428. break;
  429. case TFLOAT:
  430. res->fval = l.ival*r.fval;
  431. break;
  432. default:
  433. error("bad rhs type *");
  434. }
  435. break;
  436. case TFLOAT:
  437. switch(r.type) {
  438. case TINT:
  439. res->fval = l.fval*r.ival;
  440. break;
  441. case TFLOAT:
  442. res->fval = l.fval*r.fval;
  443. break;
  444. default:
  445. error("bad rhs type *");
  446. }
  447. break;
  448. }
  449. }
  450. void
  451. odiv(Node *n, Node *res)
  452. {
  453. Node l, r;
  454. expr(n->left, &l);
  455. expr(n->right, &r);
  456. res->fmt = l.fmt;
  457. res->op = OCONST;
  458. res->type = TFLOAT;
  459. switch(l.type) {
  460. default:
  461. error("bad lhs type /");
  462. case TINT:
  463. switch(r.type) {
  464. case TINT:
  465. res->type = TINT;
  466. if(r.ival == 0)
  467. error("zero divide");
  468. res->ival = l.ival/r.ival;
  469. break;
  470. case TFLOAT:
  471. if(r.fval == 0)
  472. error("zero divide");
  473. res->fval = l.ival/r.fval;
  474. break;
  475. default:
  476. error("bad rhs type /");
  477. }
  478. break;
  479. case TFLOAT:
  480. switch(r.type) {
  481. case TINT:
  482. res->fval = l.fval/r.ival;
  483. break;
  484. case TFLOAT:
  485. res->fval = l.fval/r.fval;
  486. break;
  487. default:
  488. error("bad rhs type /");
  489. }
  490. break;
  491. }
  492. }
  493. void
  494. omod(Node *n, Node *res)
  495. {
  496. Node l, r;
  497. expr(n->left, &l);
  498. expr(n->right, &r);
  499. res->fmt = l.fmt;
  500. res->op = OCONST;
  501. res->type = TINT;
  502. if(l.type != TINT || r.type != TINT)
  503. error("bad expr type %");
  504. res->ival = l.ival%r.ival;
  505. }
  506. void
  507. olsh(Node *n, Node *res)
  508. {
  509. Node l, r;
  510. expr(n->left, &l);
  511. expr(n->right, &r);
  512. res->fmt = l.fmt;
  513. res->op = OCONST;
  514. res->type = TINT;
  515. if(l.type != TINT || r.type != TINT)
  516. error("bad expr type <<");
  517. res->ival = l.ival<<r.ival;
  518. }
  519. void
  520. orsh(Node *n, Node *res)
  521. {
  522. Node l, r;
  523. expr(n->left, &l);
  524. expr(n->right, &r);
  525. res->fmt = l.fmt;
  526. res->op = OCONST;
  527. res->type = TINT;
  528. if(l.type != TINT || r.type != TINT)
  529. error("bad expr type >>");
  530. res->ival = (uint64_t)l.ival>>r.ival;
  531. }
  532. void
  533. olt(Node *n, Node *res)
  534. {
  535. Node l, r;
  536. expr(n->left, &l);
  537. expr(n->right, &r);
  538. res->fmt = l.fmt;
  539. res->op = OCONST;
  540. res->type = TINT;
  541. switch(l.type) {
  542. default:
  543. error("bad lhs type <");
  544. case TINT:
  545. switch(r.type) {
  546. case TINT:
  547. res->ival = l.ival < r.ival;
  548. break;
  549. case TFLOAT:
  550. res->ival = l.ival < r.fval;
  551. break;
  552. default:
  553. error("bad rhs type <");
  554. }
  555. break;
  556. case TFLOAT:
  557. switch(r.type) {
  558. case TINT:
  559. res->ival = l.fval < r.ival;
  560. break;
  561. case TFLOAT:
  562. res->ival = l.fval < r.fval;
  563. break;
  564. default:
  565. error("bad rhs type <");
  566. }
  567. break;
  568. }
  569. }
  570. void
  571. ogt(Node *n, Node *res)
  572. {
  573. Node l, r;
  574. expr(n->left, &l);
  575. expr(n->right, &r);
  576. res->fmt = 'D';
  577. res->op = OCONST;
  578. res->type = TINT;
  579. switch(l.type) {
  580. default:
  581. error("bad lhs type >");
  582. case TINT:
  583. switch(r.type) {
  584. case TINT:
  585. res->ival = l.ival > r.ival;
  586. break;
  587. case TFLOAT:
  588. res->ival = l.ival > r.fval;
  589. break;
  590. default:
  591. error("bad rhs type >");
  592. }
  593. break;
  594. case TFLOAT:
  595. switch(r.type) {
  596. case TINT:
  597. res->ival = l.fval > r.ival;
  598. break;
  599. case TFLOAT:
  600. res->ival = l.fval > r.fval;
  601. break;
  602. default:
  603. error("bad rhs type >");
  604. }
  605. break;
  606. }
  607. }
  608. void
  609. oleq(Node *n, Node *res)
  610. {
  611. Node l, r;
  612. expr(n->left, &l);
  613. expr(n->right, &r);
  614. res->fmt = 'D';
  615. res->op = OCONST;
  616. res->type = TINT;
  617. switch(l.type) {
  618. default:
  619. error("bad expr type <=");
  620. case TINT:
  621. switch(r.type) {
  622. case TINT:
  623. res->ival = l.ival <= r.ival;
  624. break;
  625. case TFLOAT:
  626. res->ival = l.ival <= r.fval;
  627. break;
  628. default:
  629. error("bad expr type <=");
  630. }
  631. break;
  632. case TFLOAT:
  633. switch(r.type) {
  634. case TINT:
  635. res->ival = l.fval <= r.ival;
  636. break;
  637. case TFLOAT:
  638. res->ival = l.fval <= r.fval;
  639. break;
  640. default:
  641. error("bad expr type <=");
  642. }
  643. break;
  644. }
  645. }
  646. void
  647. ogeq(Node *n, Node *res)
  648. {
  649. Node l, r;
  650. expr(n->left, &l);
  651. expr(n->right, &r);
  652. res->fmt = 'D';
  653. res->op = OCONST;
  654. res->type = TINT;
  655. switch(l.type) {
  656. default:
  657. error("bad lhs type >=");
  658. case TINT:
  659. switch(r.type) {
  660. case TINT:
  661. res->ival = l.ival >= r.ival;
  662. break;
  663. case TFLOAT:
  664. res->ival = l.ival >= r.fval;
  665. break;
  666. default:
  667. error("bad rhs type >=");
  668. }
  669. break;
  670. case TFLOAT:
  671. switch(r.type) {
  672. case TINT:
  673. res->ival = l.fval >= r.ival;
  674. break;
  675. case TFLOAT:
  676. res->ival = l.fval >= r.fval;
  677. break;
  678. default:
  679. error("bad rhs type >=");
  680. }
  681. break;
  682. }
  683. }
  684. void
  685. oeq(Node *n, Node *res)
  686. {
  687. Node l, r;
  688. expr(n->left, &l);
  689. expr(n->right, &r);
  690. res->fmt = 'D';
  691. res->op = OCONST;
  692. res->type = TINT;
  693. res->ival = 0;
  694. switch(l.type) {
  695. default:
  696. break;
  697. case TINT:
  698. switch(r.type) {
  699. case TINT:
  700. res->ival = l.ival == r.ival;
  701. break;
  702. case TFLOAT:
  703. res->ival = l.ival == r.fval;
  704. break;
  705. default:
  706. break;
  707. }
  708. break;
  709. case TFLOAT:
  710. switch(r.type) {
  711. case TINT:
  712. res->ival = l.fval == r.ival;
  713. break;
  714. case TFLOAT:
  715. res->ival = l.fval == r.fval;
  716. break;
  717. default:
  718. break;
  719. }
  720. break;
  721. case TSTRING:
  722. if(r.type == TSTRING) {
  723. res->ival = scmp(r.string, l.string);
  724. break;
  725. }
  726. break;
  727. case TLIST:
  728. if(r.type == TLIST) {
  729. res->ival = listcmp(l.l, r.l);
  730. break;
  731. }
  732. break;
  733. }
  734. if(n->op == ONEQ)
  735. res->ival = !res->ival;
  736. }
  737. void
  738. oland(Node *n, Node *res)
  739. {
  740. Node l, r;
  741. expr(n->left, &l);
  742. expr(n->right, &r);
  743. res->fmt = l.fmt;
  744. res->op = OCONST;
  745. res->type = TINT;
  746. if(l.type != TINT || r.type != TINT)
  747. error("bad expr type &");
  748. res->ival = l.ival&r.ival;
  749. }
  750. void
  751. oxor(Node *n, Node *res)
  752. {
  753. Node l, r;
  754. expr(n->left, &l);
  755. expr(n->right, &r);
  756. res->fmt = l.fmt;
  757. res->op = OCONST;
  758. res->type = TINT;
  759. if(l.type != TINT || r.type != TINT)
  760. error("bad expr type ^");
  761. res->ival = l.ival^r.ival;
  762. }
  763. void
  764. olor(Node *n, Node *res)
  765. {
  766. Node l, r;
  767. expr(n->left, &l);
  768. expr(n->right, &r);
  769. res->fmt = l.fmt;
  770. res->op = OCONST;
  771. res->type = TINT;
  772. if(l.type != TINT || r.type != TINT)
  773. error("bad expr type |");
  774. res->ival = l.ival|r.ival;
  775. }
  776. void
  777. ocand(Node *n, Node *res)
  778. {
  779. Node l, r;
  780. res->op = OCONST;
  781. res->type = TINT;
  782. res->ival = 0;
  783. res->fmt = 'D';
  784. expr(n->left, &l);
  785. if(bool(&l) == 0)
  786. return;
  787. expr(n->right, &r);
  788. if(bool(&r) == 0)
  789. return;
  790. res->ival = 1;
  791. }
  792. void
  793. onot(Node *n, Node *res)
  794. {
  795. Node l;
  796. res->op = OCONST;
  797. res->type = TINT;
  798. res->ival = 0;
  799. expr(n->left, &l);
  800. res->fmt = l.fmt;
  801. if(bool(&l) == 0)
  802. res->ival = 1;
  803. }
  804. void
  805. ocor(Node *n, Node *res)
  806. {
  807. Node l, r;
  808. res->op = OCONST;
  809. res->type = TINT;
  810. res->ival = 0;
  811. res->fmt = 'D';
  812. expr(n->left, &l);
  813. if(bool(&l)) {
  814. res->ival = 1;
  815. return;
  816. }
  817. expr(n->right, &r);
  818. if(bool(&r)) {
  819. res->ival = 1;
  820. return;
  821. }
  822. }
  823. void
  824. oeinc(Node *n, Node *res)
  825. {
  826. Value *v;
  827. chklval(n->left);
  828. v = n->left->sym->v;
  829. res->op = OCONST;
  830. res->type = v->type;
  831. switch(v->type) {
  832. case TINT:
  833. if(n->op == OEDEC)
  834. v->ival -= fmtsize(v);
  835. else
  836. v->ival += fmtsize(v);
  837. break;
  838. case TFLOAT:
  839. if(n->op == OEDEC)
  840. v->fval--;
  841. else
  842. v->fval++;
  843. break;
  844. default:
  845. error("bad type for pre --/++");
  846. }
  847. res->Store = v->Store;
  848. }
  849. void
  850. opinc(Node *n, Node *res)
  851. {
  852. Value *v;
  853. chklval(n->left);
  854. v = n->left->sym->v;
  855. res->op = OCONST;
  856. res->type = v->type;
  857. res->Store = v->Store;
  858. switch(v->type) {
  859. case TINT:
  860. if(n->op == OPDEC)
  861. v->ival -= fmtsize(v);
  862. else
  863. v->ival += fmtsize(v);
  864. break;
  865. case TFLOAT:
  866. if(n->op == OPDEC)
  867. v->fval--;
  868. else
  869. v->fval++;
  870. break;
  871. default:
  872. error("bad type for post --/++");
  873. }
  874. }
  875. void
  876. ocall(Node *n, Node *res)
  877. {
  878. Lsym *s;
  879. Rplace *rsav;
  880. res->op = OCONST; /* Default return value */
  881. res->type = TLIST;
  882. res->l = 0;
  883. chklval(n->left);
  884. s = n->left->sym;
  885. if(n->builtin && !s->builtin){
  886. error("no builtin %s", s->name);
  887. return;
  888. }
  889. if(s->builtin && (n->builtin || s->proc == 0)) {
  890. (*s->builtin)(res, n->right);
  891. return;
  892. }
  893. if(s->proc == 0)
  894. error("no function %s", s->name);
  895. rsav = ret;
  896. call(s->name, n->right, s->proc->left, s->proc->right, res);
  897. ret = rsav;
  898. }
  899. void
  900. ofmt(Node *n, Node *res)
  901. {
  902. expr(n->left, res);
  903. res->fmt = n->right->ival;
  904. }
  905. void
  906. owhat(Node *n, Node *res)
  907. {
  908. res->op = OCONST; /* Default return value */
  909. res->type = TLIST;
  910. res->l = 0;
  911. whatis(n->sym);
  912. }
  913. void (*expop[])(Node*, Node*) =
  914. {
  915. [ONAME] oname,
  916. [OCONST] oconst,
  917. [OMUL] omul,
  918. [ODIV] odiv,
  919. [OMOD] omod,
  920. [OADD] oadd,
  921. [OSUB] osub,
  922. [ORSH] orsh,
  923. [OLSH] olsh,
  924. [OLT] olt,
  925. [OGT] ogt,
  926. [OLEQ] oleq,
  927. [OGEQ] ogeq,
  928. [OEQ] oeq,
  929. [ONEQ] oeq,
  930. [OLAND] oland,
  931. [OXOR] oxor,
  932. [OLOR] olor,
  933. [OCAND] ocand,
  934. [OCOR] ocor,
  935. [OASGN] oasgn,
  936. [OINDM] oindm,
  937. [OEDEC] oeinc,
  938. [OEINC] oeinc,
  939. [OPINC] opinc,
  940. [OPDEC] opinc,
  941. [ONOT] onot,
  942. [OIF] 0,
  943. [ODO] 0,
  944. [OLIST] olist,
  945. [OCALL] ocall,
  946. [OCTRUCT] octruct,
  947. [OWHILE] 0,
  948. [OELSE] 0,
  949. [OHEAD] ohead,
  950. [OTAIL] otail,
  951. [OAPPEND] oappend,
  952. [ORET] 0,
  953. [OINDEX] oindex,
  954. [OINDC] oindc,
  955. [ODOT] odot,
  956. [OLOCAL] 0,
  957. [OFRAME] oframe,
  958. [OCOMPLEX] 0,
  959. [ODELETE] odelete,
  960. [OCAST] ocast,
  961. [OFMT] ofmt,
  962. [OEVAL] oeval,
  963. [OWHAT] owhat,
  964. };