mirror of
				https://github.com/eledio-devices/thirdparty-tinyexpr.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	add simple REPL
This commit is contained in:
		
							
								
								
									
										7
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								Makefile
									
									
									
									
									
								
							| @@ -4,7 +4,7 @@ LFLAGS = -lm | ||||
|  | ||||
| .PHONY = all clean | ||||
|  | ||||
| all: smoke smoke_pr bench example example2 example3 | ||||
| all: smoke smoke_pr repl bench example example2 example3 | ||||
|  | ||||
|  | ||||
| smoke: smoke.c tinyexpr.c | ||||
| @@ -15,6 +15,9 @@ smoke_pr: smoke.c tinyexpr.c | ||||
| 	$(CC) $(CCFLAGS) -DTE_POW_FROM_RIGHT -DTE_NAT_LOG -o $@ $^ $(LFLAGS) | ||||
| 	./$@ | ||||
|  | ||||
| repl: repl.o tinyexpr.o | ||||
| 	$(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS) -lreadline | ||||
|  | ||||
| bench: benchmark.o tinyexpr.o | ||||
| 	$(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS) | ||||
|  | ||||
| @@ -31,4 +34,4 @@ example3: example3.o tinyexpr.o | ||||
| 	$(CC) -c $(CCFLAGS) $< -o $@ | ||||
|  | ||||
| clean: | ||||
| 	rm -f *.o *.exe example example2 example3 bench smoke_pr smoke | ||||
| 	rm -f *.o *.exe example example2 example3 bench repl smoke_pr smoke | ||||
|   | ||||
							
								
								
									
										52
									
								
								repl.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								repl.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| #include "tinyexpr.h" | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| #include <readline/readline.h> | ||||
| #include <readline/history.h> | ||||
|  | ||||
| static int eval(const char *str) { | ||||
|     int err = 0; | ||||
|     double r = te_interp(str, &err); | ||||
|     if (err != 0) { | ||||
|         printf("Error at position %i\n", err); | ||||
|         return -1; | ||||
|     } else { | ||||
|         printf("%g\n", r); | ||||
|         return 0; | ||||
|     } | ||||
| } | ||||
|  | ||||
| static void repl() { | ||||
|     while (1) { | ||||
|         char *line = readline("> "); | ||||
|         if (line == NULL) { | ||||
|             break; | ||||
|         } else if (strcmp(line, "q") == 0 || strcmp(line, "quit") == 0) { | ||||
|             free(line); | ||||
|             break; | ||||
|         } | ||||
|  | ||||
|         if (eval(line) != -1) { | ||||
|             add_history(line); | ||||
|         } | ||||
|  | ||||
|         free(line); | ||||
|     } | ||||
| } | ||||
|  | ||||
| int main(int argc, char **argv) { | ||||
|     if (argc == 3 && strcmp(argv[1], "-e") == 0) { | ||||
|         if (eval(argv[2]) == -1) { | ||||
|             return 1; | ||||
|         } else { | ||||
|             return 0; | ||||
|         } | ||||
|     } else if (argc == 1) { | ||||
|         repl(); | ||||
|         return 0; | ||||
|     } else { | ||||
|         printf("Usage: %s\n", argv[0]); | ||||
|         printf("       %s -e <expression>\n", argv[0]); | ||||
|         return 1; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user