wtbstfu

szpont++

Aug 27th, 2025
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. // Rozszerzone typy tokenów
  7. typedef enum {
  8. TOKEN_NUMBER,
  9. TOKEN_IDENTIFIER,
  10. TOKEN_STRING, // "tekst"
  11. TOKEN_ZMIEN, // zmien
  12. TOKEN_IF, // if
  13. TOKEN_ELSE, // else
  14. TOKEN_POKAZ, // pokaz (print)
  15. TOKEN_EQUALS, // =
  16. TOKEN_EQUALS_EQUALS, // ==
  17. TOKEN_NOT_EQUALS, // !=
  18. TOKEN_LESS, // <
  19. TOKEN_GREATER, // >
  20. TOKEN_LESS_EQUAL, // <=
  21. TOKEN_GREATER_EQUAL, // >=
  22. TOKEN_PLUS,
  23. TOKEN_MINUS,
  24. TOKEN_MULTIPLY,
  25. TOKEN_DIVIDE,
  26. TOKEN_LPAREN,
  27. TOKEN_RPAREN,
  28. TOKEN_LBRACE, // {
  29. TOKEN_RBRACE, // }
  30. TOKEN_SEMICOLON,
  31. TOKEN_EOF,
  32. TOKEN_INVALID
  33. } TokenType;
  34.  
  35. // Struktura tokena
  36. typedef struct {
  37. TokenType type;
  38. double value;
  39. char* text;
  40. int position;
  41. } Token;
  42.  
  43. // Struktura lexera
  44. typedef struct {
  45. char* source;
  46. int position;
  47. int length;
  48. char current_char;
  49. } Lexer;
  50.  
  51. // Struktura zmiennej
  52. typedef struct {
  53. char* name;
  54. double value;
  55. } Variable;
  56.  
  57. // Struktura środowiska
  58. typedef struct {
  59. Variable* variables;
  60. int count;
  61. int capacity;
  62. } Environment;
  63.  
  64. // Struktura parsera
  65. typedef struct {
  66. Token* tokens;
  67. int current;
  68. int token_count;
  69. Environment* env;
  70. } Parser;
  71.  
  72. // === FUNKCJE ŚRODOWISKA ===
  73.  
  74. Environment* create_environment() {
  75. Environment* env = (Environment*)malloc(sizeof(Environment));
  76. env->variables = (Variable*)malloc(sizeof(Variable) * 10);
  77. env->count = 0;
  78. env->capacity = 10;
  79. return env;
  80. }
  81.  
  82. void set_variable(Environment* env, char* name, double value) {
  83. for (int i = 0; i < env->count; i++) {
  84. if (strcmp(env->variables[i].name, name) == 0) {
  85. env->variables[i].value = value;
  86. return;
  87. }
  88. }
  89.  
  90. if (env->count >= env->capacity) {
  91. env->capacity *= 2;
  92. env->variables = (Variable*)realloc(env->variables, sizeof(Variable) * env->capacity);
  93. }
  94.  
  95. env->variables[env->count].name = (char*)malloc(strlen(name) + 1);
  96. strcpy(env->variables[env->count].name, name);
  97. env->variables[env->count].value = value;
  98. env->count++;
  99. }
  100.  
  101. double get_variable(Environment* env, char* name) {
  102. for (int i = 0; i < env->count; i++) {
  103. if (strcmp(env->variables[i].name, name) == 0) {
  104. return env->variables[i].value;
  105. }
  106. }
  107. printf("Error: Variable '%s' not found\n", name);
  108. return 0;
  109. }
  110.  
  111. int variable_exists(Environment* env, char* name) {
  112. for (int i = 0; i < env->count; i++) {
  113. if (strcmp(env->variables[i].name, name) == 0) {
  114. return 1;
  115. }
  116. }
  117. return 0;
  118. }
  119.  
  120. // === FUNKCJE LEXERA (ROZSZERZONE) ===
  121.  
  122. Lexer* create_lexer(char* source) {
  123. Lexer* lexer = (Lexer*)malloc(sizeof(Lexer));
  124. lexer->source = source;
  125. lexer->position = 0;
  126. lexer->length = strlen(source);
  127. lexer->current_char = lexer->position < lexer->length ? lexer->source[0] : '\0';
  128. return lexer;
  129. }
  130.  
  131. void advance(Lexer* lexer) {
  132. lexer->position++;
  133. if (lexer->position >= lexer->length) {
  134. lexer->current_char = '\0';
  135. } else {
  136. lexer->current_char = lexer->source[lexer->position];
  137. }
  138. }
  139.  
  140. void skip_whitespace(Lexer* lexer) {
  141. while (lexer->current_char != '\0' && isspace(lexer->current_char)) {
  142. advance(lexer);
  143. }
  144. }
  145.  
  146. double read_number(Lexer* lexer) {
  147. char number_str[100];
  148. int index = 0;
  149.  
  150. while (lexer->current_char != '\0' && isdigit(lexer->current_char)) {
  151. number_str[index++] = lexer->current_char;
  152. advance(lexer);
  153. }
  154.  
  155. if (lexer->current_char == '.') {
  156. number_str[index++] = lexer->current_char;
  157. advance(lexer);
  158.  
  159. while (lexer->current_char != '\0' && isdigit(lexer->current_char)) {
  160. number_str[index++] = lexer->current_char;
  161. advance(lexer);
  162. }
  163. }
  164.  
  165. number_str[index] = '\0';
  166. return atof(number_str);
  167. }
  168.  
  169. char* read_identifier(Lexer* lexer) {
  170. char* identifier = (char*)malloc(100);
  171. int index = 0;
  172.  
  173. while (lexer->current_char != '\0' &&
  174. (isalnum(lexer->current_char) || lexer->current_char == '_')) {
  175. identifier[index++] = lexer->current_char;
  176. advance(lexer);
  177. }
  178.  
  179. identifier[index] = '\0';
  180. return identifier;
  181. }
  182.  
  183. char* read_string(Lexer* lexer) {
  184. char* string = (char*)malloc(100);
  185. int index = 0;
  186.  
  187. advance(lexer); // pomiń początkowy "
  188.  
  189. while (lexer->current_char != '\0' && lexer->current_char != '"') {
  190. string[index++] = lexer->current_char;
  191. advance(lexer);
  192. }
  193.  
  194. if (lexer->current_char == '"') {
  195. advance(lexer); // pomiń końcowy "
  196. }
  197.  
  198. string[index] = '\0';
  199. return string;
  200. }
  201.  
  202. Token get_next_token(Lexer* lexer) {
  203. Token token;
  204.  
  205. while (lexer->current_char != '\0') {
  206. if (isspace(lexer->current_char)) {
  207. skip_whitespace(lexer);
  208. continue;
  209. }
  210.  
  211. // Liczby
  212. if (isdigit(lexer->current_char)) {
  213. token.type = TOKEN_NUMBER;
  214. token.value = read_number(lexer);
  215. token.position = lexer->position;
  216. token.text = NULL;
  217. return token;
  218. }
  219.  
  220. // Stringi
  221. if (lexer->current_char == '"') {
  222. token.type = TOKEN_STRING;
  223. token.text = read_string(lexer);
  224. token.value = 0;
  225. token.position = lexer->position;
  226. return token;
  227. }
  228.  
  229. // Identyfikatory i słowa kluczowe
  230. if (isalpha(lexer->current_char) || lexer->current_char == '_') {
  231. char* identifier = read_identifier(lexer);
  232. token.position = lexer->position;
  233. token.value = 0;
  234.  
  235. // Sprawdź słowa kluczowe
  236. if (strcmp(identifier, "zmien") == 0) {
  237. token.type = TOKEN_ZMIEN;
  238. } else if (strcmp(identifier, "if") == 0) {
  239. token.type = TOKEN_IF;
  240. } else if (strcmp(identifier, "else") == 0) {
  241. token.type = TOKEN_ELSE;
  242. } else if (strcmp(identifier, "pokaz") == 0) {
  243. token.type = TOKEN_POKAZ;
  244. } else {
  245. token.type = TOKEN_IDENTIFIER;
  246. }
  247. token.text = identifier;
  248. return token;
  249. }
  250.  
  251. // Operatory dwuznakowe
  252. if (lexer->current_char == '=' &&
  253. lexer->position + 1 < lexer->length &&
  254. lexer->source[lexer->position + 1] == '=') {
  255. token.type = TOKEN_EQUALS_EQUALS;
  256. token.value = 0;
  257. token.position = lexer->position;
  258. token.text = "==";
  259. advance(lexer);
  260. advance(lexer);
  261. return token;
  262. }
  263.  
  264. if (lexer->current_char == '!' &&
  265. lexer->position + 1 < lexer->length &&
  266. lexer->source[lexer->position + 1] == '=') {
  267. token.type = TOKEN_NOT_EQUALS;
  268. token.value = 0;
  269. token.position = lexer->position;
  270. token.text = "!=";
  271. advance(lexer);
  272. advance(lexer);
  273. return token;
  274. }
  275.  
  276. if (lexer->current_char == '<' &&
  277. lexer->position + 1 < lexer->length &&
  278. lexer->source[lexer->position + 1] == '=') {
  279. token.type = TOKEN_LESS_EQUAL;
  280. token.value = 0;
  281. token.position = lexer->position;
  282. token.text = "<=";
  283. advance(lexer);
  284. advance(lexer);
  285. return token;
  286. }
  287.  
  288. if (lexer->current_char == '>' &&
  289. lexer->position + 1 < lexer->length &&
  290. lexer->source[lexer->position + 1] == '=') {
  291. token.type = TOKEN_GREATER_EQUAL;
  292. token.value = 0;
  293. token.position = lexer->position;
  294. token.text = ">=";
  295. advance(lexer);
  296. advance(lexer);
  297. return token;
  298. }
  299.  
  300. // Pojedyncze znaki
  301. switch (lexer->current_char) {
  302. case '=':
  303. token.type = TOKEN_EQUALS;
  304. token.value = 0;
  305. token.position = lexer->position;
  306. token.text = "=";
  307. advance(lexer);
  308. return token;
  309.  
  310. case '<':
  311. token.type = TOKEN_LESS;
  312. token.value = 0;
  313. token.position = lexer->position;
  314. token.text = "<";
  315. advance(lexer);
  316. return token;
  317.  
  318. case '>':
  319. token.type = TOKEN_GREATER;
  320. token.value = 0;
  321. token.position = lexer->position;
  322. token.text = ">";
  323. advance(lexer);
  324. return token;
  325.  
  326. case '+':
  327. token.type = TOKEN_PLUS;
  328. token.value = 0;
  329. token.position = lexer->position;
  330. token.text = "+";
  331. advance(lexer);
  332. return token;
  333.  
  334. case '-':
  335. token.type = TOKEN_MINUS;
  336. token.value = 0;
  337. token.position = lexer->position;
  338. token.text = "-";
  339. advance(lexer);
  340. return token;
  341.  
  342. case '*':
  343. token.type = TOKEN_MULTIPLY;
  344. token.value = 0;
  345. token.position = lexer->position;
  346. token.text = "*";
  347. advance(lexer);
  348. return token;
  349.  
  350. case '/':
  351. token.type = TOKEN_DIVIDE;
  352. token.value = 0;
  353. token.position = lexer->position;
  354. token.text = "/";
  355. advance(lexer);
  356. return token;
  357.  
  358. case '(':
  359. token.type = TOKEN_LPAREN;
  360. token.value = 0;
  361. token.position = lexer->position;
  362. token.text = "(";
  363. advance(lexer);
  364. return token;
  365.  
  366. case ')':
  367. token.type = TOKEN_RPAREN;
  368. token.value = 0;
  369. token.position = lexer->position;
  370. token.text = ")";
  371. advance(lexer);
  372. return token;
  373.  
  374. case '{':
  375. token.type = TOKEN_LBRACE;
  376. token.value = 0;
  377. token.position = lexer->position;
  378. token.text = "{";
  379. advance(lexer);
  380. return token;
  381.  
  382. case '}':
  383. token.type = TOKEN_RBRACE;
  384. token.value = 0;
  385. token.position = lexer->position;
  386. token.text = "}";
  387. advance(lexer);
  388. return token;
  389.  
  390. case ';':
  391. token.type = TOKEN_SEMICOLON;
  392. token.value = 0;
  393. token.position = lexer->position;
  394. token.text = ";";
  395. advance(lexer);
  396. return token;
  397.  
  398. default:
  399. token.type = TOKEN_INVALID;
  400. token.value = 0;
  401. token.position = lexer->position;
  402. token.text = NULL;
  403. advance(lexer);
  404. return token;
  405. }
  406. }
  407.  
  408. token.type = TOKEN_EOF;
  409. token.value = 0;
  410. token.position = lexer->position;
  411. token.text = "EOF";
  412. return token;
  413. }
  414.  
  415. // === FUNKCJE PARSERA ===
  416.  
  417. Parser* create_parser(Token* tokens, int token_count, Environment* env) {
  418. Parser* parser = (Parser*)malloc(sizeof(Parser));
  419. parser->tokens = tokens;
  420. parser->current = 0;
  421. parser->token_count = token_count;
  422. parser->env = env;
  423. return parser;
  424. }
  425.  
  426. Token current_token(Parser* parser) {
  427. if (parser->current >= parser->token_count) {
  428. Token eof = {TOKEN_EOF, 0, "EOF", -1};
  429. return eof;
  430. }
  431. return parser->tokens[parser->current];
  432. }
  433.  
  434. void next_token(Parser* parser) {
  435. if (parser->current < parser->token_count) {
  436. parser->current++;
  437. }
  438. }
  439.  
  440. int expect_token(Parser* parser, TokenType expected) {
  441. Token token = current_token(parser);
  442. if (token.type == expected) {
  443. next_token(parser);
  444. return 1;
  445. }
  446. return 0;
  447. }
  448.  
  449. // === FUNKCJE PARSOWANIA WYRAŻEŃ ===
  450.  
  451. double parse_expression(Parser* parser);
  452. double parse_comparison(Parser* parser);
  453.  
  454. double parse_factor(Parser* parser) {
  455. Token token = current_token(parser);
  456.  
  457. if (token.type == TOKEN_NUMBER) {
  458. next_token(parser);
  459. return token.value;
  460. }
  461.  
  462. if (token.type == TOKEN_IDENTIFIER) {
  463. next_token(parser);
  464. if (variable_exists(parser->env, token.text)) {
  465. return get_variable(parser->env, token.text);
  466. } else {
  467. printf("Error: Variable '%s' not defined\n", token.text);
  468. return 0;
  469. }
  470. }
  471.  
  472. if (token.type == TOKEN_LPAREN) {
  473. next_token(parser);
  474. double result = parse_comparison(parser);
  475. expect_token(parser, TOKEN_RPAREN);
  476. return result;
  477. }
  478.  
  479. if (token.type == TOKEN_MINUS) {
  480. next_token(parser);
  481. return -parse_factor(parser);
  482. }
  483.  
  484. printf("Error: Unexpected token in expression\n");
  485. return 0;
  486. }
  487.  
  488. double parse_term(Parser* parser) {
  489. double result = parse_factor(parser);
  490.  
  491. while (1) {
  492. Token token = current_token(parser);
  493.  
  494. if (token.type == TOKEN_MULTIPLY) {
  495. next_token(parser);
  496. result *= parse_factor(parser);
  497. } else if (token.type == TOKEN_DIVIDE) {
  498. next_token(parser);
  499. double divisor = parse_factor(parser);
  500. if (divisor == 0) {
  501. printf("Error: Division by zero!\n");
  502. return 0;
  503. }
  504. result /= divisor;
  505. } else {
  506. break;
  507. }
  508. }
  509.  
  510. return result;
  511. }
  512.  
  513. double parse_expression(Parser* parser) {
  514. double result = parse_term(parser);
  515.  
  516. while (1) {
  517. Token token = current_token(parser);
  518.  
  519. if (token.type == TOKEN_PLUS) {
  520. next_token(parser);
  521. result += parse_term(parser);
  522. } else if (token.type == TOKEN_MINUS) {
  523. next_token(parser);
  524. result -= parse_term(parser);
  525. } else {
  526. break;
  527. }
  528. }
  529.  
  530. return result;
  531. }
  532.  
  533. // NOWA FUNKCJA - parsowanie porównań
  534. double parse_comparison(Parser* parser) {
  535. double left = parse_expression(parser);
  536.  
  537. Token token = current_token(parser);
  538.  
  539. if (token.type == TOKEN_EQUALS_EQUALS) {
  540. next_token(parser);
  541. double right = parse_expression(parser);
  542. return (left == right) ? 1.0 : 0.0;
  543. } else if (token.type == TOKEN_NOT_EQUALS) {
  544. next_token(parser);
  545. double right = parse_expression(parser);
  546. return (left != right) ? 1.0 : 0.0;
  547. } else if (token.type == TOKEN_LESS) {
  548. next_token(parser);
  549. double right = parse_expression(parser);
  550. return (left < right) ? 1.0 : 0.0;
  551. } else if (token.type == TOKEN_GREATER) {
  552. next_token(parser);
  553. double right = parse_expression(parser);
  554. return (left > right) ? 1.0 : 0.0;
  555. } else if (token.type == TOKEN_LESS_EQUAL) {
  556. next_token(parser);
  557. double right = parse_expression(parser);
  558. return (left <= right) ? 1.0 : 0.0;
  559. } else if (token.type == TOKEN_GREATER_EQUAL) {
  560. next_token(parser);
  561. double right = parse_expression(parser);
  562. return (left >= right) ? 1.0 : 0.0;
  563. }
  564.  
  565. return left;
  566. }
  567.  
  568. // === FUNKCJE PARSOWANIA INSTRUKCJI ===
  569.  
  570. void parse_statement(Parser* parser);
  571. void parse_block(Parser* parser);
  572.  
  573. void parse_var_declaration(Parser* parser) {
  574. expect_token(parser, TOKEN_ZMIEN);
  575.  
  576. Token name_token = current_token(parser);
  577. if (name_token.type != TOKEN_IDENTIFIER) {
  578. printf("Error: Expected variable name after 'zmien'\n");
  579. return;
  580. }
  581. next_token(parser);
  582.  
  583. if (!expect_token(parser, TOKEN_EQUALS)) {
  584. printf("Error: Expected '=' after variable name\n");
  585. return;
  586. }
  587.  
  588. double value = parse_comparison(parser);
  589. set_variable(parser->env, name_token.text, value);
  590.  
  591. printf("Variable '%s' = %.6g\n", name_token.text, value);
  592. }
  593.  
  594. void parse_pokaz_statement(Parser* parser) {
  595. expect_token(parser, TOKEN_POKAZ);
  596.  
  597. Token token = current_token(parser);
  598.  
  599. if (token.type == TOKEN_STRING) {
  600. printf("%s\n", token.text);
  601. next_token(parser);
  602. } else {
  603. double value = parse_comparison(parser);
  604. printf("%.6g\n", value);
  605. }
  606. }
  607.  
  608. void parse_if_statement(Parser* parser) {
  609. expect_token(parser, TOKEN_IF);
  610.  
  611. if (!expect_token(parser, TOKEN_LPAREN)) {
  612. printf("Error: Expected '(' after 'if'\n");
  613. return;
  614. }
  615.  
  616. double condition = parse_comparison(parser);
  617.  
  618. if (!expect_token(parser, TOKEN_RPAREN)) {
  619. printf("Error: Expected ')' after condition\n");
  620. return;
  621. }
  622.  
  623. if (condition != 0.0) { // warunek prawdziwy
  624. if (current_token(parser).type == TOKEN_LBRACE) {
  625. parse_block(parser);
  626. } else {
  627. parse_statement(parser);
  628. }
  629.  
  630. // Pomiń else jeśli istnieje
  631. if (current_token(parser).type == TOKEN_ELSE) {
  632. next_token(parser);
  633. if (current_token(parser).type == TOKEN_LBRACE) {
  634. // Pomiń blok else
  635. expect_token(parser, TOKEN_LBRACE);
  636. int brace_count = 1;
  637. while (brace_count > 0 && current_token(parser).type != TOKEN_EOF) {
  638. if (current_token(parser).type == TOKEN_LBRACE) brace_count++;
  639. if (current_token(parser).type == TOKEN_RBRACE) brace_count--;
  640. next_token(parser);
  641. }
  642. } else {
  643. // Pomiń pojedynczą instrukcję else
  644. while (current_token(parser).type != TOKEN_EOF &&
  645. current_token(parser).type != TOKEN_RBRACE) {
  646. next_token(parser);
  647. }
  648. }
  649. }
  650. } else { // warunek fałszywy
  651. // Pomiń blok if
  652. if (current_token(parser).type == TOKEN_LBRACE) {
  653. expect_token(parser, TOKEN_LBRACE);
  654. int brace_count = 1;
  655. while (brace_count > 0 && current_token(parser).type != TOKEN_EOF) {
  656. if (current_token(parser).type == TOKEN_LBRACE) brace_count++;
  657. if (current_token(parser).type == TOKEN_RBRACE) brace_count--;
  658. next_token(parser);
  659. }
  660. } else {
  661. // Pomiń pojedynczą instrukcję
  662. while (current_token(parser).type != TOKEN_EOF &&
  663. current_token(parser).type != TOKEN_RBRACE &&
  664. current_token(parser).type != TOKEN_ELSE) {
  665. next_token(parser);
  666. }
  667. }
  668.  
  669. // Wykonaj else jeśli istnieje
  670. if (current_token(parser).type == TOKEN_ELSE) {
  671. next_token(parser);
  672. if (current_token(parser).type == TOKEN_LBRACE) {
  673. parse_block(parser);
  674. } else {
  675. parse_statement(parser);
  676. }
  677. }
  678. }
  679. }
  680.  
  681. void parse_statement(Parser* parser) {
  682. Token token = current_token(parser);
  683.  
  684. if (token.type == TOKEN_ZMIEN) {
  685. parse_var_declaration(parser);
  686. } else if (token.type == TOKEN_POKAZ) {
  687. parse_pokaz_statement(parser);
  688. } else if (token.type == TOKEN_IF) {
  689. parse_if_statement(parser);
  690. } else {
  691. // Wyrażenie do obliczenia
  692. double result = parse_comparison(parser);
  693. printf("Result: %.6g\n", result);
  694. }
  695. }
  696.  
  697. void parse_block(Parser* parser) {
  698. expect_token(parser, TOKEN_LBRACE);
  699.  
  700. while (current_token(parser).type != TOKEN_RBRACE &&
  701. current_token(parser).type != TOKEN_EOF) {
  702. parse_statement(parser);
  703. }
  704.  
  705. expect_token(parser, TOKEN_RBRACE);
  706. }
  707.  
  708. // === FUNKCJE GŁÓWNE ===
  709.  
  710. Token* tokenize_all(char* input, int* token_count) {
  711. Lexer* lexer = create_lexer(input);
  712. Token* tokens = (Token*)malloc(sizeof(Token) * 100);
  713. *token_count = 0;
  714.  
  715. Token token;
  716. do {
  717. token = get_next_token(lexer);
  718. tokens[*token_count] = token;
  719. (*token_count)++;
  720. } while (token.type != TOKEN_EOF && *token_count < 100);
  721.  
  722. free(lexer);
  723. return tokens;
  724. }
  725.  
  726. void execute_statement(char* input, Environment* env) {
  727. int token_count;
  728. Token* tokens = tokenize_all(input, &token_count);
  729.  
  730. if (token_count == 0 || tokens[0].type == TOKEN_EOF) {
  731. free(tokens);
  732. return;
  733. }
  734.  
  735. Parser* parser = create_parser(tokens, token_count, env);
  736. parse_statement(parser);
  737.  
  738. free(tokens);
  739. free(parser);
  740. }
  741.  
  742. void show_variables(Environment* env) {
  743. if (env->count == 0) {
  744. printf("No variables defined\n");
  745. return;
  746. }
  747.  
  748. printf("Variables:\n");
  749. for (int i = 0; i < env->count; i++) {
  750. printf(" %s = %.6g\n", env->variables[i].name, env->variables[i].value);
  751. }
  752. }
  753.  
  754. void test_conditionals() {
  755. printf("=== AUTOMATIC TESTS WITH CONDITIONALS ===\n");
  756.  
  757. Environment* env = create_environment();
  758.  
  759. execute_statement("zmien x = 10", env);
  760. execute_statement("zmien y = 5", env);
  761.  
  762. printf("Testing: if (x > y) { pokaz \"x is bigger\" }\n");
  763. execute_statement("if (x > y) { pokaz \"x is bigger\" }", env);
  764.  
  765. printf("Testing: if (y > x) { pokaz \"impossible\" } else { pokaz \"y is smaller\" }\n");
  766. execute_statement("if (y > x) { pokaz \"impossible\" } else { pokaz \"y is smaller\" }", env);
  767.  
  768. printf("Testing: if (x == 10) { pokaz x }\n");
  769. execute_statement("if (x == 10) { pokaz x }", env);
  770.  
  771. printf("Testing: if (x != y) { pokaz \"different\" }\n");
  772. execute_statement("if (x != y) { pokaz \"different\" }", env);
  773. }
  774.  
  775. int main() {
  776. printf("=== CALCULATOR WITH CONDITIONALS ===\n");
  777. printf("Now your language supports if/else statements!\n\n");
  778.  
  779. printf("Commands:\n");
  780. printf(" zmien name = expression - create variable\n");
  781. printf(" pokaz value - print value\n");
  782. printf(" pokaz \"text\" - print text\n");
  783. printf(" if (condition) { ... } - conditional execution\n");
  784. printf(" if (condition) {...} else {...} - if/else\n");
  785. printf(" expression - calculate\n");
  786. printf(" vars - show all variables\n");
  787. printf(" quit - exit\n\n");
  788.  
  789. printf("Comparison operators: == != < > <= >=\n\n");
  790.  
  791. test_conditionals();
  792.  
  793. Environment* env = create_environment();
  794. char input[256];
  795.  
  796. printf("\n=== INTERACTIVE MODE ===\n");
  797.  
  798. while (1) {
  799. printf("> ");
  800. if (fgets(input, sizeof(input), stdin) == NULL) {
  801. break;
  802. }
  803.  
  804. input[strcspn(input, "\n")] = 0;
  805.  
  806. if (strcmp(input, "quit") == 0) {
  807. break;
  808. } else if (strcmp(input, "vars") == 0) {
  809. show_variables(env);
  810. } else if (strlen(input) > 0) {
  811. execute_statement(input, env);
  812. }
  813. }
  814.  
  815. printf("\nCongratulations! Your language now has conditionals!\n");
  816. printf("Next: loops, functions, arrays...\n");
  817. return 0;
  818. }
Advertisement
Add Comment
Please, Sign In to add comment