]> ocean-lang.org Git - ocean/blobdiff - csrc/oceani.mdc
oceani: add the option for "const" sections
[ocean] / csrc / oceani.mdc
index de899a2524d91d79f740bd2582874aa5fe7a018d..58d5d32009f297102359c4feabeed21c7ea4f15f 100644 (file)
@@ -114,6 +114,7 @@ structures can be used.
                struct token_config config;
                char *file_name;
                int parse_error;
+               struct exec *prog;
                ## parse context
        };
 
@@ -179,7 +180,6 @@ structures can be used.
                        },
                };
                int doprint=0, dotrace=0, doexec=1, brackets=0;
-               struct exec **prog;
                int opt;
                while ((opt = getopt_long(argc, argv, options, long_options, NULL))
                       != -1) {
@@ -223,32 +223,31 @@ structures can be used.
                                        break;
                        }
                        if (ss)
-                               prog = parse_oceani(ss->code, &context.config,
-                                                   dotrace ? stderr : NULL);
+                               parse_oceani(ss->code, &context.config,
+                                            dotrace ? stderr : NULL);
                        else {
                                fprintf(stderr, "oceani: cannot find section %s\n",
                                        section);
                                exit(1);
                        }
                } else
-                       prog = parse_oceani(s->code, &context.config,
-                                   dotrace ? stderr : NULL);
-               if (!prog) {
-                       fprintf(stderr, "oceani: fatal parser error.\n");
+                       parse_oceani(s->code, &context.config,
+                                    dotrace ? stderr : NULL);
+               if (!context.prog) {
+                       fprintf(stderr, "oceani: no program found.\n");
                        context.parse_error = 1;
                }
-               if (prog && doprint)
-                       print_exec(*prog, 0, brackets);
-               if (prog && doexec && !context.parse_error) {
-                       if (!analyse_prog(*prog, &context)) {
+               if (context.prog && doprint)
+                       print_exec(context.prog, 0, brackets);
+               if (context.prog && doexec && !context.parse_error) {
+                       if (!analyse_prog(context.prog, &context)) {
                                fprintf(stderr, "oceani: type error in program - not running.\n");
                                exit(1);
                        }
-                       interp_prog(*prog, argv+optind+1);
+                       interp_prog(context.prog, argv+optind+1);
                }
-               if (prog) {
-                       free_exec(*prog);
-                       free(prog);
+               if (context.prog) {
+                       free_exec(context.prog);
                }
                while (s) {
                        struct section *t = s->next;
@@ -1082,6 +1081,8 @@ no longer be primary.
 
                        v = t->previous;
                        free_value(t->val);
+                       if (t->min_depth == 0)
+                               free_exec(t->where_decl);
                        free(t);
                }
        }
@@ -3544,12 +3545,111 @@ defined.
                break;
        }
 
+### Top level structure
+
+All the language elements so far can be used in various places.  Now
+it is time to clarify what those places are.
+
+At the top level of a file there will be a number of declarations.
+Many of the things that can be declared haven't been described yet,
+such as functions, procedures, imports, named types, and probably
+more.
+For now there are two sorts of things that can appear at the top
+level.  They are predefined constants and the main program.  While the
+syntax will allow the main program to appear multiple times, that will
+trigger an error if it is actually attempted.
+
+The various declarations do not return anything.  They store the
+various declarations in the parse context.
+
+###### Parser: grammar
+
+       $void
+       Ocean -> DeclarationList
+
+       DeclarationList -> Declaration
+               | DeclarationList Declaration
+
+       Declaration -> DeclareConstant
+               | DeclareProgram
+               | NEWLINE
+
+       ## top level grammar
+
+### The `const` section
+
+As well as being defined in with the code that uses them, constants
+can be declared at the top level.  These have full-file scope, so they
+are always `InScope`.  The value of a top level constant can be given
+as an expression, and this is evaluated immediately rather than in the
+later interpretation stage.  Once we add functions to the language, we
+will need rules concern which, if any, can be used to define a top
+level constant.
+
+Constants are defined in a sectiont that starts with the reserved word
+`const` and then has a block with a list of assignment statements.
+For syntactic consistency, these must use the double-colon syntax to
+make it clear that they are constants.  Type can also be given: if
+not, the type will be determined during analysis, as with other
+constants.
+
+###### top level grammar
+
+       DeclareConstant -> const Open ConstList Close
+               | const Open Newlines ConstList Close
+               | const Open SimpleConstList }
+               | const Open Newlines SimpleConstList }
+               | const : ConstList
+               | const SimpleConstList
+
+       ConstList -> ComplexConsts
+       ComplexConsts -> ComplexConst ComplexConsts
+               | ComplexConst
+       ComplexConst -> SimpleConstList NEWLINE
+       SimpleConstList -> Const ; SimpleConstList
+               | Const
+               | Const ; SimpleConstList ;
+
+       $*type
+       CType -> Type   ${ $0 = $<1; }$
+               |       ${ $0 = NULL; }$
+       $void
+       Const -> IDENTIFIER :: CType = Expression ${ {
+               int ok;
+               struct variable *v;
+
+               v = var_decl(config2context(config), $1.txt);
+               if (v) {
+                       struct var *var = new_pos(var, $1);
+                       v->where_decl = var;
+                       v->where_set = var;
+                       var->var = v;
+                       v->constant = 1;
+               } else {
+                       v = var_ref(config2context(config), $1.txt);
+                       tok_err(config2context(config), "error: name already declared", &$1);
+                       type_err(config2context(config), "info: this is where '%v' was first declared",
+                                v->where_decl, NULL, 0, NULL);
+               }
+               do {
+                       ok = 1;
+                       propagate_types($5, config2context(config), &ok, $3, 0);
+               } while (ok == 2);
+               if (!ok)
+                       config2context(config)->parse_error = 1;
+               else if (v) {
+                       v->val = interp_exec($5);
+               }
+       } }$
+
+
 ### Finally the whole program.
 
 Somewhat reminiscent of Pascal a (current) Ocean program starts with
 the keyword "program" and a list of variable names which are assigned
 values from command line arguments.  Following this is a `block` which
-is the code to execute.
+is the code to execute.  Unlike Pascal, constants and other
+declarations come *before* the program.
 
 As this is the top level, several things are handled a bit
 differently.
@@ -3560,7 +3660,17 @@ analysis is a bit more interesting at this level.
 ###### Binode types
        Program,
 
-###### Parser: grammar
+###### top level grammar
+
+       DeclareProgram -> Program ${ {
+               struct parse_context *c = config2context(config);
+               if (c->prog)
+                       type_err(c, "Program defined a second time",
+                                $1, NULL, 0, NULL);
+               else
+                       c->prog = $<1;
+       } }$
+
 
        $*binode
        Program -> program OpenScope Varlist Block OptNL ${
@@ -3694,8 +3804,18 @@ Fibonacci, and performs a binary search for a number.
 
 ###### test: hello
 
+       const:
+               pi ::= 3.1415926
+               four ::= 2 + 2 ; five ::= 10/2
+       const pie ::= "I like Pie";
+               cake ::= "The cake is"
+                 ++ " a lie"
+
        program A B:
                print "Hello World, what lovely oceans you have!"
+               print "are there", five, "?"
+               print pi, pie, "but", cake
+
                /* When a variable is defined in both branches of an 'if',
                 * and used afterwards, the variables are merged.
                 */