src/Tools/Code/code_target.ML
changeset 33969 1e7ca47c6c3d
parent 33522 737589bb9bb8
child 34021 e820ed4bfd94
equal deleted inserted replaced
33968:f94fb13ecbb3 33969:1e7ca47c6c3d
    44   val add_syntax_class: string -> class -> string option -> theory -> theory
    44   val add_syntax_class: string -> class -> string option -> theory -> theory
    45   val add_syntax_inst: string -> string * class -> bool -> theory -> theory
    45   val add_syntax_inst: string -> string * class -> bool -> theory -> theory
    46   val add_syntax_tyco: string -> string -> tyco_syntax option -> theory -> theory
    46   val add_syntax_tyco: string -> string -> tyco_syntax option -> theory -> theory
    47   val add_syntax_const: string -> string -> proto_const_syntax option -> theory -> theory
    47   val add_syntax_const: string -> string -> proto_const_syntax option -> theory -> theory
    48   val add_reserved: string -> string -> theory -> theory
    48   val add_reserved: string -> string -> theory -> theory
       
    49   val add_include: string -> string * (string * string list) option -> theory -> theory
    49 end;
    50 end;
    50 
    51 
    51 structure Code_Target : CODE_TARGET =
    52 structure Code_Target : CODE_TARGET =
    52 struct
    53 struct
    53 
    54 
   163 fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x;
   164 fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x;
   164 fun the_module_alias (Target { module_alias , ... }) = module_alias;
   165 fun the_module_alias (Target { module_alias , ... }) = module_alias;
   165 
   166 
   166 val abort_allowed = snd o CodeTargetData.get;
   167 val abort_allowed = snd o CodeTargetData.get;
   167 
   168 
   168 fun assert_target thy target =
   169 fun assert_target thy target = if Symtab.defined (fst (CodeTargetData.get thy)) target
   169   case Symtab.lookup (fst (CodeTargetData.get thy)) target
   170   then target
   170    of SOME data => target
   171   else error ("Unknown code target language: " ^ quote target);
   171     | NONE => error ("Unknown code target language: " ^ quote target);
       
   172 
   172 
   173 fun put_target (target, seri) thy =
   173 fun put_target (target, seri) thy =
   174   let
   174   let
   175     val lookup_target = Symtab.lookup (fst (CodeTargetData.get thy));
   175     val lookup_target = Symtab.lookup (fst (CodeTargetData.get thy));
   176     val _ = case seri
   176     val _ = case seri
   236       else error ("No such type constructor: " ^ quote tyco);
   236       else error ("No such type constructor: " ^ quote tyco);
   237   in tyco end;
   237   in tyco end;
   238 
   238 
   239 fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
   239 fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
   240 
   240 
   241 fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy =
   241 fun gen_add_syntax_class prep_class target raw_class raw_syn thy =
   242   let
   242   let
   243     val class = prep_class thy raw_class;
   243     val class = prep_class thy raw_class;
   244   in case raw_syn
   244   in case raw_syn
   245    of SOME syntax =>
   245    of SOME syntax =>
   246       thy
   246       thy
   316             val cs = map (read_const thy) raw_cs;
   316             val cs = map (read_const thy) raw_cs;
   317           in Symtab.update (name, (str content, cs)) incls end
   317           in Symtab.update (name, (str content, cs)) incls end
   318       | add (name, NONE) incls = Symtab.delete name incls;
   318       | add (name, NONE) incls = Symtab.delete name incls;
   319   in map_includes target (add args) thy end;
   319   in map_includes target (add args) thy end;
   320 
   320 
   321 val add_include = gen_add_include Code.check_const;
   321 val add_include = gen_add_include (K I);
   322 val add_include_cmd = gen_add_include Code.read_const;
   322 val add_include_cmd = gen_add_include Code.read_const;
   323 
   323 
   324 fun add_module_alias target (thyname, modlname) =
   324 fun add_module_alias target (thyname, modlname) =
   325   let
   325   let
   326     val xs = Long_Name.explode modlname;
   326     val xs = Long_Name.explode modlname;
   353   #-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name --
   353   #-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name --
   354         (zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")"));
   354         (zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")"));
   355 
   355 
   356 in
   356 in
   357 
   357 
   358 val add_syntax_class = gen_add_syntax_class cert_class (K I);
   358 val add_syntax_class = gen_add_syntax_class cert_class;
   359 val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco;
   359 val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco;
   360 val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
   360 val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
   361 val add_syntax_const = gen_add_syntax_const (K I);
   361 val add_syntax_const = gen_add_syntax_const (K I);
   362 val allow_abort = gen_allow_abort (K I);
   362 val allow_abort = gen_allow_abort (K I);
   363 val add_reserved = add_reserved;
   363 val add_reserved = add_reserved;
   364 
   364 val add_include = add_include;
   365 val add_syntax_class_cmd = gen_add_syntax_class read_class Code.read_const;
   365 
       
   366 val add_syntax_class_cmd = gen_add_syntax_class read_class;
   366 val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco;
   367 val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco;
   367 val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
   368 val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
   368 val add_syntax_const_cmd = gen_add_syntax_const Code.read_const;
   369 val add_syntax_const_cmd = gen_add_syntax_const Code.read_const;
   369 val allow_abort_cmd = gen_allow_abort Code.read_const;
   370 val allow_abort_cmd = gen_allow_abort Code.read_const;
   370 
   371 
   604 fun shell_command thyname cmd = Toplevel.program (fn _ =>
   605 fun shell_command thyname cmd = Toplevel.program (fn _ =>
   605   (use_thy thyname; case Scan.read OuterLex.stopper (P.!!! code_exprP)
   606   (use_thy thyname; case Scan.read OuterLex.stopper (P.!!! code_exprP)
   606     ((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd)
   607     ((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd)
   607    of SOME f => (writeln "Now generating code..."; f (theory thyname))
   608    of SOME f => (writeln "Now generating code..."; f (theory thyname))
   608     | NONE => error ("Bad directive " ^ quote cmd)))
   609     | NONE => error ("Bad directive " ^ quote cmd)))
   609   handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure;
   610   handle Runtime.TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure;
   610 
   611 
   611 end; (*local*)
   612 end; (*local*)
   612 
   613 
   613 end; (*struct*)
   614 end; (*struct*)