1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
|
/*$Id: lang_spice.cc $ -*- C++ -*-
* Copyright (C) 2006 Albert Davis
* Author: Albert Davis <aldavis@gnu.org>
*
* This file is part of "Gnucap", the Gnu Circuit Analysis Package
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
//testing=script 2015.01.27
#include "u_nodemap.h"
#include "globals.h"
#include "u_status.h"
#include "c_comand.h"
#include "d_dot.h"
#include "d_coment.h"
#include "e_subckt.h"
#include "u_lang.h"
#include "d_logic.h"
#include "bm.h"
/*--------------------------------------------------------------------------*/
namespace {
/*--------------------------------------------------------------------------*/
class LANG_SPICE_BASE : public LANGUAGE {
public:
LANG_SPICE_BASE() {}
~LANG_SPICE_BASE() {}
enum EOB {NO_EXIT_ON_BLANK, EXIT_ON_BLANK};
public: // override virtual, used by callback
std::string arg_front()const override {return " ";}
std::string arg_mid()const override {return "=";}
std::string arg_back()const override {return "";}
public: // override virtual, called by commands
DEV_COMMENT* parse_comment(CS&, DEV_COMMENT*)override;
DEV_DOT* parse_command(CS&, DEV_DOT*)override;
MODEL_CARD* parse_paramset(CS&, MODEL_CARD*)override;
BASE_SUBCKT* parse_module(CS&, BASE_SUBCKT*)override;
COMPONENT* parse_instance(CS&, COMPONENT*)override;
std::string find_type_in_string(CS&)override;
public: // "local?", called by own commands
void parse_module_body(CS&, BASE_SUBCKT*, CARD_LIST*, const std::string&,
EOB, const std::string&);
private: // local
void parse_type(CS&, CARD*);
void parse_args(CS&, CARD*);
void parse_label(CS&, CARD*);
void parse_ports(CS&, COMPONENT*, int minnodes, int start, int num_nodes, bool all_new);
private: // compatibility hacks
void parse_element_using_obsolete_callback(CS&, COMPONENT*);
void parse_logic_using_obsolete_callback(CS&, COMPONENT*);
private: // override virtual, called by print_item
void print_paramset(OMSTREAM&, const MODEL_CARD*)override;
void print_module(OMSTREAM&, const BASE_SUBCKT*)override;
void print_instance(OMSTREAM&, const COMPONENT*)override;
void print_comment(OMSTREAM&, const DEV_COMMENT*)override;
void print_command(OMSTREAM&, const DEV_DOT*)override;
private: // local
void print_args(OMSTREAM&, const MODEL_CARD*);
void print_type(OMSTREAM&, const COMPONENT*);
void print_args(OMSTREAM&, const COMPONENT*);
void print_label(OMSTREAM&, const COMPONENT*);
void print_ports(OMSTREAM&, const COMPONENT*);
};
/*--------------------------------------------------------------------------*/
class LANG_SPICE : public LANG_SPICE_BASE {
public:
LANG_SPICE() {}
~LANG_SPICE() {}
std::string name()const override {return "spice";}
bool case_insensitive()const override {return true;}
UNITS units()const override {return uSPICE;}
void parse_top_item(CS&, CARD_LIST*)override;
} lang_spice;
DISPATCHER<LANGUAGE>::INSTALL
ds(&language_dispatcher, lang_spice.name(), &lang_spice);
/*--------------------------------------------------------------------------*/
class LANG_ACS : public LANG_SPICE_BASE {
public:
LANG_ACS() {}
~LANG_ACS() {}
std::string name()const override {return "acs";}
bool case_insensitive()const override {return true;}
UNITS units()const override {return uSPICE;}
} lang_acs;
DISPATCHER<LANGUAGE>::INSTALL
da(&language_dispatcher, lang_acs.name(), &lang_acs);
/*--------------------------------------------------------------------------*/
DEV_COMMENT p0;
DISPATCHER<CARD>::INSTALL
d0(&device_dispatcher, ";|#|*|'|\"|dev_comment", &p0);
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
static void skip_pre_stuff(CS& cmd)
{
cmd.skipbl();
while (cmd.umatch(CKT_PROMPT)) {untested();
/* skip any number of copies of the prompt */
}
cmd.umatch(ANTI_COMMENT); /* skip mark so spice ignores but gnucap reads */
}
/*--------------------------------------------------------------------------*/
/* count_ports: figure out how many ports
* returns the number of ports
* side effect: "CS" is advanced to past the ports, ready for what follows
*/
static int count_ports(CS& cmd, int maxnodes, int minnodes, int leave_tail, int start)
{
assert(start < maxnodes);
assert(minnodes <= maxnodes);
int num_nodes = 0;
std::vector<size_t> spots;
int paren = cmd.skip1b('(');
int i = start;
// loop over the tokens to try to guess where the nodes end
// and other stuff begins
spots.push_back(cmd.cursor());
for (;;) {
++i;
//cmd.skiparg();
std::string node_name;
cmd >> node_name;
spots.push_back(cmd.cursor());
if (paren && cmd.skip1b(')')) {
num_nodes = i;
break;
}else if (cmd.is_end()) {
// found the end, no '='
if (i <= minnodes) {
num_nodes = i;
}else if (i <= minnodes + leave_tail) {
num_nodes = minnodes;
}else if (i <= maxnodes + leave_tail) {
num_nodes = i - leave_tail;
}else{
num_nodes = maxnodes;
}
break;
}else if (cmd.skip1b("({})")) {
// found '(', it's past the end of nodes
if (i > maxnodes + leave_tail) {
num_nodes = maxnodes;
}else{
num_nodes = i - leave_tail;
}
break;
}else if (cmd.skip1b('=')) {
// found '=', it's past the end of nodes
if (i > maxnodes + leave_tail + 1) {
num_nodes = maxnodes;
}else{
num_nodes = i - leave_tail - 1;
}
break;
}else{
}
}
if (num_nodes < start) {untested();
cmd.reset(spots.back());
throw Exception("what's this?");
}else{
}
cmd.reset(spots[static_cast<size_t>(num_nodes-start)]);
//cmd.warn(bDANGER, "past-nodes?");
//BUG// assert fails on current controlled sources with (node node dev) syntax
// it's ok with (node node) dev syntax or node node dev syntax
assert(num_nodes <= maxnodes);
return num_nodes;
}
/*--------------------------------------------------------------------------*/
/* parse_ports: parse circuit connections from input string
* fills in the rest of the array with 0
* returns the number of nodes actually read
* The purpose of this complexity is to handle the variants of Spice formats,
* which might have keys between nodes,
* an unknown number of nodes with unknown number of args and no delimeters.
* Consider: X1 a b c d e f g h i j k
* Clearly (?) a,b,c,d,e are nodes, f is a subckt name, the rest are args.
* But how do you know?
*
* Args:
* maxnodes: the maximum number of port nodes to parse.
* Stop here, even if it seems there should be more.
*
* minnodes: the minimum number of port nodes to parse.
* It is an error if there are fewer than this.
*
* leave_tail: The number of arguments that are not nodes,
* and don't have equal signs, following.
* It is an aid to distinguishing the nodes from things that follow.
* minnodes wins over leave_tail, but leave_tail wins over maxnodes.
* The above example would need leave_tail=6 to parse correctly.
* This one: X1 a b c d e f g=h i=j k
* where a,b,c,d,e are nodes, f is a subckt or model identifier
* would parse correctly with leave_tail=1.
* Usual values for leave_tail are 0 or 1.
*
* start: start at this number, used for continuing after HSPICE kluge.
* Other than that, the only useful value for start is 0.
*
* all_new: All node names must be new (not already defined) and unique.
* This is used for the header line in .subckt, which starts clean.
* The main benefit is to reject duplicates.
*/
void LANG_SPICE_BASE::parse_ports(CS& cmd, COMPONENT* x, int minnodes,
int start, int num_nodes, bool all_new)
{
assert(x);
int paren = cmd.skip1b('(');
int index = start;
size_t here1 = cmd.cursor();
try{
for (;;) {
here1 = cmd.cursor();
if (paren && cmd.skip1b(')')) {
--paren;
break; // done. have closing paren.
}else if (index >= num_nodes) {
break; // done. have maxnodes.
}else if (!cmd.more()) {untested();
break; // done. premature end of line.
}else if (OPT::keys_between_nodes &&
(cmd.umatch("poly ")
|| cmd.umatch("pwl ")
|| cmd.umatch("vccap ")
|| cmd.umatch("vcg ")
|| cmd.umatch("vcr "))
) {
cmd.reset(here1);
break; // done, found reserved word between nodes
}else{
//----------------------
size_t here = cmd.cursor();
std::string node_name;
cmd >> node_name;
if (cmd.stuck(&here)) {untested();
// didn't move, probably a terminator.
throw Exception("bad node name");
}else{
// legal node name, store it.
x->set_port_by_index(index, node_name);
}
//----------------------
if (!(x->node_is_connected(index))) {untested();
break; // illegal node name, might be proper exit.
}else{
if (all_new) {
if (x->node_is_grounded(index)) {
cmd.warn(bDANGER, here1, "node 0 not allowed here");
}else if (x->subckt() && x->subckt()->nodes()->how_many() != index+1) {
cmd.warn(bDANGER, here1, "duplicate port name, skipping");
}else{
++index;
}
}else{
++index;
}
}
}
}
}catch (Exception& e) {untested();
cmd.warn(bDANGER, here1, e.message());
}
if (index < minnodes) {untested();
cmd.warn(bDANGER, "need " + to_string(minnodes-index) +" more nodes");
}else{
}
if (paren != 0) {untested();
cmd.warn(bWARNING, "need )");
}else{
}
//assert(x->_net_nodes == index);
// ground unused input nodes
for (int iii = index; iii < minnodes; ++iii) {untested();
x->set_port_to_ground(iii);
}
//assert(x->_net_nodes >= index);
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::parse_element_using_obsolete_callback(CS& cmd, COMPONENT* x)
{
assert(x);
ELEMENT* xx = dynamic_cast<ELEMENT*>(x);
assert(xx);
{
size_t here = cmd.cursor();
int stop_nodes = x->max_nodes() - x->num_current_ports();
int num_nodes = count_ports(cmd, stop_nodes, 0, 0, 0);
// max min tail already_got
cmd.reset(here);
parse_ports(cmd, x, 0, 0, num_nodes, false);
// min already_got
}
int gotnodes = x->_net_nodes;
COMMON_COMPONENT* c = NULL;
if (gotnodes < x->min_nodes()) {
// HSPICE compatibility kluge.
// The device type or function type could be stuck between the nodes.
xx->skip_dev_type(cmd); // (redundant)
c = EVAL_BM_ACTION_BASE::parse_func_type(cmd);
{
size_t here = cmd.cursor();
int num_nodes = count_ports(cmd, x->max_nodes(), x->min_nodes(), 0, gotnodes);
cmd.reset(here);
parse_ports(cmd, x, x->min_nodes(), gotnodes, num_nodes, false);
}
}else{
// Normal mode. nodes first, then data.
}
if (!c) {
xx->skip_dev_type(cmd); // (redundant)
c = bm_dispatcher.clone("eval_bm_cond");
}else{
}
if (!c) {untested();
c = bm_dispatcher.clone("eval_bm_value");
}else{
}
assert(c);
// we have a blank common of the most general type
// (or HSPICE kluge)
// let it continue parsing
size_t here = cmd.cursor();
c->parse_common_obsolete_callback(cmd); //BUG//callback
if (cmd.stuck(&here)) {untested();
cmd.warn(bDANGER, "needs a value");
}else{
}
// At this point, there is ALWAYS a common "c", which may have more
// commons attached to it. Try to reduce its complexity.
// "c->deflate()" may return "c" or some simplification of "c".
COMMON_COMPONENT* dc = c->deflate();
// dc == deflated_common
// It might be just "c".
// It might be something else that is simpler but equivalent.
if (dc->is_trivial()) {
assert(dynamic_cast<EVAL_BM_VALUE*>(dc));
// If it is a simple value, don't use a common.
// Just store the value directly.
xx->obsolete_move_parameters_from_common(dc);
delete c;
}else{
x->attach_common(dc);
if (dc != c) {
delete c;
}else{
}
}
cmd.check(bDANGER, "what's this?");
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::parse_logic_using_obsolete_callback(CS& cmd, COMPONENT* x)
{
assert(x);
{
size_t here = cmd.cursor();
int num_nodes = count_ports(cmd, x->max_nodes(), x->min_nodes(), x->tail_size(), 0/*start*/);
cmd.reset(here);
parse_ports(cmd, x, x->min_nodes(), 0/*start*/, num_nodes, false);
}
int incount = x->net_nodes() - x->min_nodes() + 1;
assert(incount > 0);
std::string modelname = cmd.ctos(TOKENTERM);
COMMON_LOGIC* common = 0;
if (cmd.umatch("and " )) {itested();common = new LOGIC_AND;}
else if (cmd.umatch("nand ")) {common = new LOGIC_NAND;}
else if (cmd.umatch("or " )) {itested();common = new LOGIC_OR;}
else if (cmd.umatch("nor " )) {common = new LOGIC_NOR;}
else if (cmd.umatch("xor " )) {itested();common = new LOGIC_XOR;}
else if (cmd.umatch("xnor ")) {itested();common = new LOGIC_XNOR;}
else if (cmd.umatch("inv " )) {common = new LOGIC_INV;}
else {untested();
cmd.warn(bWARNING,"need and,nand,or,nor,xor,xnor,inv");
common=new LOGIC_NONE;
}
assert(common);
common->incount = incount;
common->set_modelname(modelname);
x->attach_common(common);
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::parse_type(CS& cmd, CARD* x)
{
assert(x);
std::string new_type;
cmd >> new_type;
x->set_dev_type(new_type);
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::parse_args(CS& cmd, CARD* x)
{
assert(x);
COMPONENT* xx = dynamic_cast<COMPONENT*>(x);
cmd >> "params:"; // optional, skip it.
if (!x->use_obsolete_callback_parse()) {
int paren = cmd.skip1b('(');
if (xx && cmd.is_float()) { // simple unnamed value
std::string value;
cmd >> value;
x->set_param_by_name(xx->value_name(), value);
}else if (cmd.match1("'{")) { // quoted unnamed value
std::string value;
cmd >> value; // strips off the quotes
value = '{' + value + '}'; // put them back
x->set_param_by_name(xx->value_name(), value);
}else{ // only name=value pairs
}
size_t here = cmd.cursor();
for (int i=0; ; ++i) {
if (paren && cmd.skip1b(')')) {
break;
}else if (!cmd.more()) {
break;
}else{
std::string Name = cmd.ctos("=", "", "");
cmd >> '=';
std::string value = cmd.ctos(",=;)", "\"'{(", "\"'})");
size_t there = here;
if (cmd.stuck(&here)) {untested();
break;
}else{
try{
if (value == "") {untested();
cmd.warn(bDANGER, there, x->long_label() + ": " + Name + " has no value?");
}else{
}
x->set_param_by_name(Name, value);
}catch (Exception_No_Match&) {untested();
cmd.warn(bDANGER, there, x->long_label() + ": bad parameter " + Name + " ignored");
}
}
}
}
}else if (MODEL_CARD* pp = dynamic_cast<MODEL_CARD*>(x)) {
// used only for "table"
int paren = cmd.skip1b('(');
bool in_error = false;
for (;;) {
size_t here = cmd.cursor();
pp->parse_params_obsolete_callback(cmd); //BUG//callback//
if (!cmd.more()) {
break;
}else if (paren && cmd.skip1b(')')) {untested();
break;
}else if (cmd.stuck(&here)) {untested();
if (in_error) {untested();
// so you don't get two errors on name = value.
cmd.skiparg();
in_error = false;
}else{untested();
cmd.warn(bDANGER, "bad paramerter -- ignored");
cmd.skiparg().skip1b("=");
in_error = true;
}
}else{
in_error = false;
}
}
}else{untested();
// using obsolete_callback
}
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::parse_label(CS& cmd, CARD* x)
{
assert(x);
std::string my_name;
if (cmd >> my_name) {
x->set_label(my_name);
}else{untested();
x->set_label(x->id_letter() + std::string("_unnamed")); //BUG// not unique
cmd.warn(bDANGER, "label required");
}
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
DEV_COMMENT* LANG_SPICE_BASE::parse_comment(CS& cmd, DEV_COMMENT* x)
{
assert(x);
x->set(cmd.fullstring());
return x;
}
/*--------------------------------------------------------------------------*/
DEV_DOT* LANG_SPICE_BASE::parse_command(CS& cmd, DEV_DOT* x)
{
assert(x);
x->set(cmd.fullstring());
CARD_LIST* scope = (x->owner()) ? x->owner()->subckt() : &CARD_LIST::card_list;
cmd.reset();
skip_pre_stuff(cmd);
size_t here = cmd.cursor();
std::string s;
cmd >> s;
cmd.reset(here);
if (!command_dispatcher[s]) {
cmd.skip();
++here;
}else{
}
CMD::cmdproc(cmd, scope);
delete x;
return NULL;
}
/*--------------------------------------------------------------------------*/
MODEL_CARD* LANG_SPICE_BASE::parse_paramset(CS& cmd, MODEL_CARD* x)
{
assert(x);
cmd.reset();
cmd >> ".model ";
parse_label(cmd, x);
parse_type(cmd, x);
parse_args(cmd, x);
cmd.check(bWARNING, "what's this?");
return x;
}
/*--------------------------------------------------------------------------*/
BASE_SUBCKT* LANG_SPICE_BASE::parse_module(CS& cmd, BASE_SUBCKT* x)
{
assert(x);
// header
cmd.reset();
(cmd >> ".subckt |.macro ");
parse_label(cmd, x);
{
size_t here = cmd.cursor();
int num_nodes = count_ports(cmd, x->max_nodes(), x->min_nodes(),
0/*no unnamed par*/, 0/*start*/);
cmd.reset(here);
parse_ports(cmd, x, x->min_nodes(), 0/*start*/, num_nodes, true/*all new*/);
}
x->subckt()->params()->parse(cmd);
// body
parse_module_body(cmd, x, x->subckt(), name() + "-subckt>", NO_EXIT_ON_BLANK, ".ends |.eom ");
return x;
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::parse_module_body(CS& cmd, BASE_SUBCKT* x, CARD_LIST* Scope,
const std::string& prompt, EOB exit_on_blank, const std::string& exit_key)
{
try {
for (;;) {
cmd.get_line(prompt);
if ((exit_on_blank==EXIT_ON_BLANK && cmd.is_end())
|| cmd.umatch(exit_key)) {
break;
}else{
skip_pre_stuff(cmd);
new__instance(cmd, x, Scope);
}
}
}catch (Exception_End_Of_Input& e) {
}
}
/*--------------------------------------------------------------------------*/
COMPONENT* LANG_SPICE_BASE::parse_instance(CS& cmd, COMPONENT* x)
{
try {
assert(x);
cmd.reset().umatch(ANTI_COMMENT);
// ACS dot type specifier
if (cmd.skip1b('.')) {
parse_type(cmd, x);
}else{
}
parse_label(cmd, x);
if (x->use_obsolete_callback_parse()) {
parse_element_using_obsolete_callback(cmd, x);
}else if (DEV_LOGIC* xx = dynamic_cast<DEV_LOGIC*>(x)) {
parse_logic_using_obsolete_callback(cmd, xx);
}else{
{
size_t here = cmd.cursor();
int num_nodes = count_ports(cmd, x->max_nodes(), x->min_nodes(), x->tail_size(), 0);
cmd.reset(here);
parse_ports(cmd, x, x->min_nodes(), 0/*start*/, num_nodes, false);
}
if (x->print_type_in_spice()) {
parse_type(cmd, x);
}else{
}
parse_args(cmd, x);
}
}catch (Exception& e) {untested();
cmd.warn(bDANGER, e.message());
}
return x;
}
/*--------------------------------------------------------------------------*/
std::string LANG_SPICE_BASE::find_type_in_string(CS& cmd)
{
cmd.umatch(ANTI_COMMENT); /* skip mark so spice ignores but gnucap reads */
size_t here = cmd.cursor();
std::string s;
char id_letter = cmd.peek();
if (OPT::case_insensitive) {
id_letter = static_cast<char>(toupper(id_letter));
}else{
}
switch (id_letter) {
case '\0':untested();
s = "";
break;
case '.':
cmd >> s;
cmd.reset(here);
if (!command_dispatcher[s]) {
cmd.skip();
++here;
s = s.substr(1);
}else{
}
break;
case 'G':
here = cmd.cursor();
if (cmd.scan("vccap |vcg |vcr |vccs ")) {
s = cmd.trimmed_last_match();
}else{
s = "G";
}
break;
default:
s = id_letter;
break;
}
cmd.reset(here);
return s;
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE::parse_top_item(CS& cmd, CARD_LIST* Scope)
{
if (0 && cmd.is_file()
&& cmd.is_first_read()
&& (Scope == &CARD_LIST::card_list)
&& (Scope->is_empty())
&& (head == "'")) {untested(); //BUG// ugly hack
cmd.get_line("gnucap-spice-title>");
head = cmd.fullstring();
IO::mstdout << head << '\n';
}else{
cmd.get_line("gnucap-spice>");
new__instance(cmd, NULL, Scope);
}
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
static char fix_case(char c)
{
return ((OPT::case_insensitive) ? (static_cast<char>(tolower(c))) : (c));
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::print_paramset(OMSTREAM& o, const MODEL_CARD* x)
{
assert(x);
o << ".model " << x->short_label() << ' ' << x->dev_type() << " (";
print_args(o, x);
o << ")\n";
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::print_module(OMSTREAM& o, const BASE_SUBCKT* x)
{
assert(x);
assert(x->subckt());
o << ".subckt " << x->short_label();
print_ports(o, x);
o << '\n';
for (CARD_LIST::const_iterator
ci = x->subckt()->begin(); ci != x->subckt()->end(); ++ci) {
print_item(o, *ci);
}
o << ".ends " << x->short_label() << "\n";
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::print_instance(OMSTREAM& o, const COMPONENT* x)
{
print_label(o, x);
print_ports(o, x);
print_type(o, x);
print_args(o, x);
o << '\n';
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::print_comment(OMSTREAM& o, const DEV_COMMENT* x)
{
assert(x);
if (x->comment()[1] != '+') {
if (x->comment()[0] != '*') {
o << "*";
}else{
}
o << x->comment() << '\n';
}else{
}
// Suppress printing of comment lines starting with "*+".
// These are generated as a way to display calculated values.
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::print_command(OMSTREAM& o, const DEV_DOT* x)
{itested();
assert(x);
o << x->s() << '\n';
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::print_args(OMSTREAM& o, const MODEL_CARD* x)
{
assert(x);
if (x->use_obsolete_callback_print()) {
x->print_args_obsolete_callback(o, this); //BUG//callback//
}else{
for (int ii = x->param_count() - 1; ii >= x->param_count_dont_print(); --ii) {
if (x->param_is_printable(ii)) {
std::string arg = " " + x->param_name(ii) + "=" + x->param_value(ii);
o << arg;
}else{
}
}
}
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::print_type(OMSTREAM& o, const COMPONENT* x)
{
assert(x);
if (x->print_type_in_spice()) {
o << " " << x->dev_type();
}else if (fix_case(x->short_label()[0]) != fix_case(x->id_letter())) {
o << " " << x->dev_type();
}else{
// don't print type
}
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::print_args(OMSTREAM& o, const COMPONENT* x)
{
assert(x);
o << ' ';
if (x->use_obsolete_callback_print()) {
x->print_args_obsolete_callback(o, this); //BUG//callback//
}else{
for (int ii = x->param_count() - 1; ii >= x->param_count_dont_print(); --ii) {
if (x->param_is_printable(ii)) {
if ((ii != x->param_count() - 1) || (x->param_name(ii) != x->value_name())) {
// skip name if plain value
o << " " << x->param_name(ii) << "=";
}else{
}
o << x->param_value(ii);
}else{
}
}
}
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::print_label(OMSTREAM& o, const COMPONENT* x)
{
assert(x);
o << x->short_label();
}
/*--------------------------------------------------------------------------*/
void LANG_SPICE_BASE::print_ports(OMSTREAM& o, const COMPONENT* x)
{
assert(x);
o << " ( ";
std::string sep = "";
for (int ii = 0; x->port_exists(ii); ++ii) {
o << sep << x->port_value(ii);
sep = " ";
}
for (int ii = 0; x->current_port_exists(ii); ++ii) {
o << sep << x->current_port_value(ii);
sep = " ";
}
o << " )";
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
class CMD_MODEL : public CMD {
void do_it(CS& cmd, CARD_LIST* Scope)override {
// already got "model"
std::string my_name, base_name;
cmd >> my_name;
size_t here1 = cmd.cursor();
cmd >> base_name;
// "level" kluge ....
// if there is a "level" keyword, with integer argument,
// tack that onto the given modelname and look for that
cmd.skip1b('(');
int level = 0;
{
size_t here = cmd.cursor();
scan_get(cmd, "level ", &level);
if (!cmd.stuck(&here)) {
char buf[20];
sprintf(buf, "%u", level);
base_name += buf;
}else{
}
}
const MODEL_CARD* p = model_dispatcher[base_name];
if (p) {
CARD* cl = p->clone();
MODEL_CARD* new_card = dynamic_cast<MODEL_CARD*>(cl);
if (new_card) {
assert(!new_card->owner());
lang_spice.parse_paramset(cmd, new_card);
Scope->push_back(new_card);
}else{untested();
delete(cl);
cmd.warn(bDANGER, here1, "model: base has incorrect type");
}
}else{
cmd.warn(bDANGER, here1, "model: \"" + base_name + "\" no match");
}
}
} p1;
DISPATCHER<CMD>::INSTALL d1(&command_dispatcher, ".model", &p1);
/*--------------------------------------------------------------------------*/
class CMD_SUBCKT : public CMD {
void do_it(CS& cmd, CARD_LIST* Scope)override {
BASE_SUBCKT* new_module = dynamic_cast<BASE_SUBCKT*>(device_dispatcher.clone("subckt"));
assert(new_module);
assert(!new_module->owner());
assert(new_module->subckt());
assert(new_module->subckt()->is_empty());
assert(!new_module->is_device());
lang_spice.parse_module(cmd, new_module);
Scope->push_back(new_module);
}
} p2;
DISPATCHER<CMD>::INSTALL d2(&command_dispatcher, ".subckt|.macro", &p2);
/*--------------------------------------------------------------------------*/
enum Skip_Header {NO_HEADER, SKIP_HEADER};
/*--------------------------------------------------------------------------*/
/* getmerge: actually do the work for "get", "merge", etc.
*/
static void getmerge(CS& cmd, Skip_Header skip_header, CARD_LIST* Scope)
{
::status.get.reset().start();
assert(Scope);
std::string file_name, section_name;
cmd >> file_name;
bool echoon = false; /* echo on/off flag (echo as read from file) */
bool liston = false; /* list on/off flag (list actual values) */
bool quiet = false; /* don't echo title */
size_t here = cmd.cursor();
do{
ONE_OF
|| Get(cmd, "echo", &echoon)
|| Get(cmd, "list", &liston)
|| Get(cmd, "quiet", &quiet)
|| Get(cmd, "section", §ion_name)
;
}while (cmd.more() && !cmd.stuck(&here));
if (cmd.more()) {
cmd >> section_name;
}else{
}
cmd.check(bWARNING, "need section, echo, list, or quiet");
CS file(CS::_INC_FILE, file_name);
if (skip_header) { // get and store the header line
file.get_line(">>>>");
head = file.fullstring();
if (!quiet) {
IO::mstdout << head << '\n';
}else{untested();
}
}else{
}
if (section_name == "") {
lang_spice.parse_module_body(file, NULL, Scope, ">>>>", lang_spice.NO_EXIT_ON_BLANK, ".end ");
}else{
try {
for (;;) {
file.get_line("lib " + section_name + '>');
if (file.umatch(".lib " + section_name + ' ')) {
lang_spice.parse_module_body(file, NULL, Scope, section_name,
lang_spice.NO_EXIT_ON_BLANK, ".endl {" + section_name + "}");
}else{
// skip it
}
}
}catch (Exception_End_Of_Input& e) {
}
}
::status.get.stop();
}
/*--------------------------------------------------------------------------*/
/* cmd_lib: lib command
*/
class CMD_LIB : public CMD {
public:
void do_it(CS& cmd, CARD_LIST* Scope)override {
size_t here = cmd.cursor();
std::string section_name, more_stuff;
cmd >> section_name >> more_stuff;
if (more_stuff != "") {
cmd.reset(here);
getmerge(cmd, NO_HEADER, Scope);
}else{
for (;;) {
cmd.get_line(section_name + '>');
if (cmd.umatch(".endl {" + section_name + "}")) {
break;
}else{
// skip it
}
}
}
}
} p33;
DISPATCHER<CMD>::INSTALL d33(&command_dispatcher, ".lib|lib", &p33);
/*--------------------------------------------------------------------------*/
/* cmd_include: include command
* as get or run, but do not clear first, inherit the run-mode.
*/
class CMD_INCLUDE : public CMD {
public:
void do_it(CS& cmd, CARD_LIST* Scope)override {untested();
getmerge(cmd, NO_HEADER, Scope);
}
} p3;
DISPATCHER<CMD>::INSTALL d3(&command_dispatcher, ".include", &p3);
/*--------------------------------------------------------------------------*/
/* cmd_merge: merge command
* as get, but do not clear first
*/
class CMD_MERGE : public CMD {
public:
void do_it(CS& cmd, CARD_LIST* Scope)override {untested();
SET_RUN_MODE xx(rPRESET);
getmerge(cmd, NO_HEADER, Scope);
}
} p4;
DISPATCHER<CMD>::INSTALL d4(&command_dispatcher, ".merge|merge", &p4);
/*--------------------------------------------------------------------------*/
/* cmd_run: "<" and "<<" commands
* run in batch mode. Spice format.
* "<<" clears old circuit first, "<" does not
* get circuit from file, execute dot cards in sequence
*/
class CMD_RUN : public CMD {
public:
void do_it(CS& cmd, CARD_LIST* Scope)override {
while (cmd.match1('<')) {untested();
command("clear", Scope);
cmd.skip();
cmd.skipbl();
}
SET_RUN_MODE xx(rSCRIPT);
getmerge(cmd, SKIP_HEADER, Scope);
}
} p5;
DISPATCHER<CMD>::INSTALL d5(&command_dispatcher, "<", &p5);
/*--------------------------------------------------------------------------*/
/* cmd_get: get command
* get circuit from a file, after clearing the old one
* preset, but do not execute "dot cards"
*/
class CMD_GET : public CMD {
public:
void do_it(CS& cmd, CARD_LIST* Scope)override {
SET_RUN_MODE xx(rPRESET);
command("clear", Scope);
getmerge(cmd, SKIP_HEADER, Scope);
}
} p6;
DISPATCHER<CMD>::INSTALL d6(&command_dispatcher, ".get|get", &p6);
/*--------------------------------------------------------------------------*/
/* cmd_build: build command
* get circuit description direct from keyboard (or stdin if redirected)
* Command syntax: build <before>
* Bare command: add to end of list
* If there is an arg: add before that element
* null line exits this mode
* preset, but do not execute "dot cards"
*/
class CMD_BUILD : public CMD {
public:
void do_it(CS& cmd, CARD_LIST* Scope)override {untested();
SET_RUN_MODE xx(rPRESET);
::status.get.reset().start();
lang_spice.parse_module_body(cmd, NULL, Scope, ">", lang_spice.EXIT_ON_BLANK, ". ");
::status.get.stop();
}
} p7;
DISPATCHER<CMD>::INSTALL d7(&command_dispatcher, ".build|build", &p7);
/*--------------------------------------------------------------------------*/
class CMD_SPICE : public CMD {
public:
void do_it(CS&, CARD_LIST* Scope)override {
command("options lang=spice", Scope);
}
} p8;
DISPATCHER<CMD>::INSTALL d8(&command_dispatcher, "spice", &p8);
/*--------------------------------------------------------------------------*/
class CMD_ACS : public CMD {
public:
void do_it(CS&, CARD_LIST* Scope)override {itested();
command("options lang=acs", Scope);
}
} p9;
DISPATCHER<CMD>::INSTALL d9(&command_dispatcher, "acs", &p9);
/*--------------------------------------------------------------------------*/
class CMD_ENDC : public CMD {
public:
void do_it(CS&, CARD_LIST* Scope)override {untested();
if (OPT::language == &lang_acs) {untested();
command("options lang=spice", Scope);
}else{untested();
}
}
} p88;
DISPATCHER<CMD>::INSTALL d88(&command_dispatcher, ".endc", &p88);
/*--------------------------------------------------------------------------*/
class CMD_CONTROL : public CMD {
public:
void do_it(CS&, CARD_LIST* Scope)override {untested();
if (OPT::language == &lang_spice) {untested();
command("options lang=acs", Scope);
}else{untested();
}
}
} p99;
DISPATCHER<CMD>::INSTALL d99(&command_dispatcher, ".control", &p99);
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// vim:ts=8:sw=2:noet:
|