5 Commit-ok 292b08e80f ... 69ee18e3f4

Szerző SHA1 Üzenet Dátum
  Steven 69ee18e3f4 keywords rename and parser eat fix 9 éve
  Steven e4bc434d28 nitpicks 9 éve
  Steven 2c1faac2d2 number ast node type and EOF in comments fix 9 éve
  soud 0b183bebc6 formatting 9 éve
  soud 31560ccb2a EOF checking in comment reading 9 éve
10 módosított fájl, 62 hozzáadás és 32 törlés
  1. 1 1
      Makefile
  2. 5 3
      README.md
  3. 1 1
      examples/fibonacci.er
  4. 2 2
      examples/parse.er
  5. 15 0
      src/ast.c
  6. 6 0
      src/ast.h
  7. 2 1
      src/erupt.c
  8. 16 10
      src/lexer.c
  9. 14 14
      src/lexer.h
  10. 0 0
      src/parser.c

+ 1 - 1
Makefile

@@ -10,7 +10,7 @@ all:
 	@-rm $(OBJFILES)
 
 install:
-	cp bin/erupt /usr/bin
+	cp bin/erupt /usr/bin/
 
 clean:
 	@rm *.o

+ 5 - 3
README.md

@@ -5,9 +5,11 @@ purpose only and should not be used in production.
 ## Usage
 ### Build
 
-    $ git clone https://github.com/soudy/Erupt
-    $ cd Erupt
-    $ make
+```bash
+$ git clone https://github.com/soudy/Erupt
+$ cd Erupt
+$ make
+```
 
 This will create the binary `bin/erupt`.
 

+ 1 - 1
examples/fibonacci.er

@@ -1,4 +1,4 @@
-fun fibo(i64 n) -> i64
+fun fibo(Int64 n) -> Int64
 {
     if n < 2 {
         return 1;

+ 2 - 2
examples/parse.er

@@ -14,11 +14,11 @@
                     i += 2    print("else stmt")
 *)
 
-mut i32 glob_var = 5;
+mut Int32 glob_var = 5;
 
 fun main
 {
-    mut i32 i = 0;
+    mut Int32 i = 0;
 
     while i < 20 {
         if odd(i) {

+ 15 - 0
src/ast.c

@@ -23,6 +23,16 @@
 #include "erupt.h"
 #include "ast.h"
 
+ast_node_t *create_number(float value)
+{
+        ast_node_t *node = smalloc(sizeof(ast_node_t));
+
+        node->type = TYPE_NUMBER;
+        node->number.value = value;
+
+        return node;
+}
+
 ast_node_t *create_var(char *name, ast_type_t data_type, bool mutable)
 {
         ast_node_t *node = smalloc(sizeof(ast_node_t));
@@ -119,6 +129,8 @@ static void destroy_node(ast_node_t *node)
                 return;
 
         switch(node->type) {
+        case TYPE_NUMBER:
+                break;
         case TYPE_VAR:
                 if (node->var.name)
                         free(node->var.name);
@@ -183,6 +195,9 @@ static void dump_node(ast_node_t *node)
                 return;
 
         switch (node->type) {
+        case TYPE_NUMBER:
+                printf("number:\n\tvalue: %f\n", node->number.value);
+                break;
         case TYPE_VAR:
                 printf("variable:\n\tname: %s\n\ttype: %u\n\tmutable:%d\n",
                        node->var.name, node->var.data_type, node->var.mutable);

+ 6 - 0
src/ast.h

@@ -61,6 +61,10 @@ struct ast_node;
 struct ast_node_list;
 
 typedef struct {
+        float value;
+} ast_number_t;
+
+typedef struct {
         char *name;
         ast_type_t data_type;
         bool mutable;
@@ -97,6 +101,7 @@ typedef struct {
 
 typedef struct ast_node {
         enum {
+                TYPE_NUMBER,
                 TYPE_VAR,
                 TYPE_PROTO,
                 TYPE_FN,
@@ -106,6 +111,7 @@ typedef struct ast_node {
         } type;
 
         union {
+                ast_number_t number;
                 ast_var_t var;
                 ast_proto_t prototype;
                 ast_fn_t fn;

+ 2 - 1
src/erupt.c

@@ -98,7 +98,6 @@ void error_printf(const char *m, const char *fmt, ...)
 void fatal_error(const char *m, const char *fmt, ...)
 {
         va_list arg;
-        verbose_printf("error found, stopping compilation\n");
 
         va_start(arg, fmt);
 
@@ -108,5 +107,7 @@ void fatal_error(const char *m, const char *fmt, ...)
 
         va_end(arg);
 
+        verbose_printf("error found, stopping compilation\n");
+
         exit(ERUPT_ERROR);
 }

+ 16 - 10
src/lexer.c

@@ -28,11 +28,11 @@ static char peek(lexer_t *lexer);
 static char eat(lexer_t *lexer);
 static void emit_token(lexer_t *lexer, tokentype type);
 static tokentype get_keyword(lexer_t *lexer);
-static bool is_int(char c);
+static bool is_number(char c);
 static bool is_ident(char c);
 static bool is_whitespace(char c);
 static void read_string(lexer_t *lexer, char id);
-static void read_int(lexer_t *lexer);
+static void read_number(lexer_t *lexer);
 static void read_ident(lexer_t *lexer);
 static void read_comment(lexer_t *lexer);
 
@@ -216,8 +216,8 @@ lexer_t *lex(char *target_file, char *source)
                 default:
                         if (is_ident(c)) {
                                 read_ident(lexer);
-                        } else if (is_int(c)) {
-                                read_int(lexer);
+                        } else if (is_number(c)) {
+                                read_number(lexer);
                         } else {
                                 emit_token(lexer, TOKEN_ERROR);
                                 file_fatal_error("erroneous token '%c' on line %zu\n",
@@ -292,9 +292,9 @@ static tokentype get_keyword(lexer_t *lexer)
         return TOKEN_IDENT;
 }
 
-static bool is_int(char c)
+static bool is_number(char c)
 {
-        return c >= '0' && c <= '9';
+        return (c >= '0' && c <= '9') || c == '.';
 }
 
 static bool is_ident(char c)
@@ -321,9 +321,9 @@ static void read_string(lexer_t *lexer, char id)
         emit_token(lexer, TOKEN_STRING);
 }
 
-static void read_int(lexer_t *lexer)
+static void read_number(lexer_t *lexer)
 {
-        while (is_int(peek(lexer)))
+        while (is_number(peek(lexer)))
                 eat(lexer);
 
         emit_token(lexer, TOKEN_NUMBER);
@@ -331,7 +331,7 @@ static void read_int(lexer_t *lexer)
 
 static void read_ident(lexer_t *lexer)
 {
-        while (is_ident(peek(lexer)) || is_int(peek(lexer)))
+        while (is_ident(peek(lexer)) || is_number(peek(lexer)))
                 eat(lexer);
 
         emit_token(lexer, get_keyword(lexer));
@@ -339,13 +339,19 @@ static void read_ident(lexer_t *lexer)
 
 static void read_comment(lexer_t *lexer)
 {
+        size_t start_line = lexer->line_n;
+
         while (1) {
+                if (peek(lexer) == EOF)
+                        file_fatal_error("unterminated comment on line %d\n", start_line);
+
                 if (peek(lexer) == '\n')
                         ++lexer->line_n;
 
                 if (eat(lexer) == '*' && peek(lexer) == ')') {
                         eat(lexer);
-                        verbose_printf("skipped comment\n");
+                        verbose_printf("skipped comment on line %zu\n",
+                                       start_line);
                         break;
                 }
         }

+ 14 - 14
src/lexer.h

@@ -26,8 +26,8 @@
 #include "token.h"
 
 #define FUNCTION_KEYWORD "fun"
-#define TRUE_KEYWORD     "true"
-#define FALSE_KEYWORD    "false"
+#define TRUE_KEYWORD     "True"
+#define FALSE_KEYWORD    "False"
 #define MUTABLE_KEYWORD  "mut"
 #define IMPORT_KEYWORD   "use"
 #define RETURN_KEYWORD   "return"
@@ -39,18 +39,18 @@
 #define UNLESS_KEYWORD   "unless"
 #define ELSE_KEYWORD     "else"
 
-#define I8_KEYWORD     "i8"
-#define I16_KEYWORD    "i16"
-#define I32_KEYWORD    "i32"
-#define I64_KEYWORD    "i64"
-#define U8_KEYWORD     "u8"
-#define U16_KEYWORD    "u16"
-#define U32_KEYWORD    "u32"
-#define U64_KEYWORD    "u64"
-#define STRING_KEYWORD "str"
-#define BOOL_KEYWORD   "bool"
-#define FLOAT_KEYWORD  "float"
-#define NONE_KEYWORD   "none"
+#define I8_KEYWORD     "Int8"
+#define I16_KEYWORD    "Int16"
+#define I32_KEYWORD    "Int32"
+#define I64_KEYWORD    "Int64"
+#define U8_KEYWORD     "Int8"
+#define U16_KEYWORD    "UInt16"
+#define U32_KEYWORD    "UInt32"
+#define U64_KEYWORD    "UInt64"
+#define STRING_KEYWORD "Str"
+#define BOOL_KEYWORD   "Bool"
+#define FLOAT_KEYWORD  "Float"
+#define NONE_KEYWORD   "None"
 
 typedef struct {
         char *target_file;

+ 0 - 0
src/parser.c


Nem az összes módosított fájl került megjelenítésre, mert túl sok fájl változott