#!/bin/sh
# -*-tcl-*-\
    exec cslush-tcl "$0" ${1+"$@"}

# Translate header file container description to C code that generates
#  the appropriate container description files
#
# Copyright (C)1996-2000 Jacques H. de Villiers <Jacques@deVilliers.com>
# Copyright (C)1996-2000 Center for Spoken Language Understanding,
#                        Oregon Graduate Institute of Science and Technology
#
# See the file "license.ogi" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# $Source: /usr/speech/CSLUrep2/src/csluc/container/parse,v $
# $Revision: 1.4 $
# $Date: 2000/08/11 00:10:30 $
# $State: Exp $
# $Locker:  $
# --------------------------------------------------------------------------

if $argc<1 {
 puts stderr "usage: $argv0 filename \[filename...\]"
 exit 1
}

set Outfile [open [file join [file dirname [lindex $argv 0]] cntrs.c] w]

set Code(include) {

#include <config.h>

#include <cf/stdlib.h>
#include <cf/string.h>
#include "dballoc.h"
#include "result.h"
#include "container.h"
#include "cntrint.h"

#ifndef EXIT_SUCCESS
# define EXIT_SUCCESS 0
#endif

}
set Code(head) {
int main(void)
}
append Code(head) "{\n  Cntr *c=dbmalloc(sizeof(Cntr));\n"
append Code(head) {
  GS_Result *res=GS_CreateResult();
  if(Cntr_Init(res)!=GS_OK) {
    fprintf(stderr,"ERROR: %s\n",res->result);
    exit(1);
  }
  Cntr_PathSet("containers",SET);
}

set Code(tail) "\n  dbfree((char *)c);\n  return EXIT_SUCCESS;\n}\n"

set W* "\[ \t\]*"
set W+ "\[ \t\]+"

proc trans pattern {
 global W* W+
 foreach {exp code} $pattern {
  lappend n [subst -nocommands -nobackslashes $exp] $code
 }
 return $n
}

set Container [trans {
 {^(typedef${W+})?struct${W+}([a-zA-Z0-9_]*)${W*}\{${W*}/\*${W*}container:([a-zA-Z0-9_\./]+)${W*}}
 {global Pattern C f
  set C(container) $3
  if {[string length $2]} {set C(struct) "struct $2"} {set C(struct) {}}
  set C(check,type) {}
  set C(check,line) {}
  set C(check,index) {}
  set C(code,0) {}
  set C(code,1) "  if(c->blocks) c->desc=dbmalloc(sizeof(Block)*c->blocks);\n"
  append C(code,1) "  else c->desc=dbmalloc(1);\n"
  append C(code,1) "  memset(c->desc,0,sizeof(Block)*c->blocks);\n"
  append C(code,1) "  c->name=strcpy(dbmalloc([expr [string length $C(container)]+1]),\"$C(container)\");\n"
  set C(code,2) {}
  set C(code,3) "\n  if(Cntr_Set(res,c,\"[file tail $filename]\")!=GS_OK) {\n"
  append C(code,3) {    fprintf(stderr,"ERROR: %s\n",res->result); exit(2);}
  append C(code,3) "\n  }\n"
  append C(code,3) "  dbfree((char *)c->desc);\n"
  append C(code,3) "  dbfree(c->name);\n"
  set C(blocks) 0
  Line $file $filename linenum $Pattern {Trace "No rule for: '$line'"
   global Error
   incr Error
  } {
   Trace "EOF in definition of $C(container)"
   exit 1
  }
 }
}]


set Pattern [trans {
 {^${W*}\}${W*}([a-zA-Z0-9_]*)${W*};${W*}$} {
  # end of the container definition - postprocess and write out C code
  #  this might match unions etc. definitions in complicated structs...
  global C Error Out
  if {![string length $C(struct)]} {set C(struct) $1}
  append C(code,0) [format "\n  /* %70.70s */\n" "-- $C(container) -------------------------------------------------------------------------------"]
  append C(code,0) "  c->length=sizeof($C(struct));\n"
  append C(code,0) "  c->blocks=$C(blocks);\n"
  append Out $C(code,0)
  regsub -all _STRUCT_ $C(code,1) $C(struct) C(code,1)
  set C(code,1) [subst -nobackslashes -nocommands $C(code,1)]
  set C(check,type) [subst -nobackslashes -nocommands $C(check,type)]
  foreach t $C(check,type) l $C(check,line) m $C(check,index) {
   switch $t {char - short - int {}
    default {
     puts stderr "$filename,$l: invalid length type \"$t $m\""
     incr Error;
    }
   }
  }
  append Out $C(code,1)
  append Out $C(code,2)
  append Out $C(code,3)
  return
 }
 
 {^${W*}$} {}

 {^${W*}/\*.*\*/${W*}$} {}
   
 {^.*;${W*}/\*${W*}hidden${W+}.*\*/} {}
 
 {^${W*}(unsigned${W+})?(char|short|int|float|double)${W+}([a-zA-Z0-9_]+);} {
  # scalar type
  global C
  set t $2
  set name $3
  set C(type,$name) $t
  set C(member,$name) $C(blocks)
  set T [string toupper $t]
  set b "  c->desc\[$C(blocks)\]"
  append C(code,1) "\n  /* $t $name */\n"
  append C(code,1) "$b.type=S_$T;\n"
  append C(code,1) "$b.d.s_$t.offset=C_OFFSET(_STRUCT_,$name);\n"
  append C(code,1) "$b.d.s_$t.length=1;\n"
  incr C(blocks)
 }

 {^${W*}(unsigned${W+})?(char|short|int|float|double)${W+}([a-zA-Z0-9_]+)\[([0-9]+)\];} {
  # static array
  global C
  set t $2
  set name $3
  set C(type,$name) $t
  set C(member,$name) $C(blocks)
  set length $4
  set T [string toupper $t]
  set b "  c->desc\[$C(blocks)\]"
  append C(code,1) "\n  /* $t $name\[$length\] */\n"
  append C(code,1) "$b.type=S_$T;\n"
  append C(code,1) "$b.d.s_$t.offset=C_OFFSET(_STRUCT_,$name\[0\]);\n"
  append C(code,1) "$b.d.s_$t.length=$length;\n"
  incr C(blocks)
 }

 {^${W*}(unsigned${W+})?(char|short|int|float|double)${W+}\*([a-zA-Z0-9_]+);${W*}/\*${W*}len:([a-zA-Z0-9_]+)${W*}} {
  # dynamically allocated array
  global C
  set t $2
  set name $3
  set C(type,$name) $t
  set C(member,$name) $C(blocks)
  set index $4
  set T [string toupper $t]
  set b "  c->desc\[$C(blocks)\]"
  append C(code,1) "\n  /* $t $name\[$index\] */\n"
  append C(code,1) "$b.type=P_$T;\n"
  append C(code,1) "$b.d.p_$t.offset=C_OFFSET(_STRUCT_,$name);\n"
  append C(code,1) "$b.d.p_$t.index=\$C(member,$index);\n"
  lappend C(check,type) \$C(type,$index)
  lappend C(check,index) $index
  lappend C(check,line) $linenum
  incr C(blocks)
 }

 {^${W*}(unsigned${W+})?([a-zA-Z0-9_]+)${W+}\*+([a-zA-Z0-9_]+);${W*}/\*${W*}dim:([a-zA-Z0-9_]+)${W+}data:([a-zA-Z0-9_]+)${W*}} {
  # dynamically allocated N-dimensional array
  global C
  set t $2
  set name $3
  set C(type,$name) $t 
  set C(member,$name) $C(blocks)
  set dim $4
  set data $5
  set T [string toupper $t]
  set b "  c->desc\[$C(blocks)\]"
  append C(code,1) "\n  /* $t $name\[$dim\[\]\] */\n"
  append C(code,1) "$b.type=P_NDIM;\n"
  append C(code,1) "$b.d.p_ndim.offset=C_OFFSET(_STRUCT_,$name);\n"
  append C(code,1) "$b.d.p_ndim.dim=\$C(member,$dim);\n"
  append C(code,1) "$b.d.p_ndim.data=\$C(member,$data);\n"
  append C(code,1) "$b.d.p_ndim.datasize=sizeof(\$C(type,$data));\n"
  incr C(blocks)
 }

 {^${W*}(unsigned${W+})?([a-zA-Z0-9_]+)${W+}\*+([a-zA-Z0-9_]+);${W*}/\*${W*}layers:([a-zA-Z0-9_]+)${W+}data:([a-zA-Z0-9_]+)${W*}} {
  # dynamically allocated N-layer neural network
  global C
  set t $2
  set name $3
  set C(type,$name) $t 
  set C(member,$name) $C(blocks)
  set layers $4
  set data $5
  set T [string toupper $t]
  set b "  c->desc\[$C(blocks)\]"
  append C(code,1) "\n  /* $t $name - $layers layer neural network */\n"
  append C(code,1) "$b.type=P_NNET;\n"
  append C(code,1) "$b.d.p_nnet.offset=C_OFFSET(_STRUCT_,$name);\n"
  append C(code,1) "$b.d.p_nnet.layers=\$C(member,$layers);\n"
  append C(code,1) "$b.d.p_nnet.data=\$C(member,$data);\n"
  append C(code,1) "$b.d.p_nnet.datasize=sizeof(\$C(type,$data));\n"
  incr C(blocks)
 }

 {^${W*}(unsigned${W+})?([a-zA-Z0-9_]+)${W+}([a-zA-Z0-9_]+);${W*}/\*${W*}container:([a-zA-Z0-9_\./]+)${W*}} {
  # single static typed structure
  global C
  set t $2
  set name $3
  set C(type,$name) $t 
  set C(member,$name) $C(blocks)
  set length 1
  set cntr $4
  set T [string toupper $t]
  set b "  c->desc\[$C(blocks)\]"
  append C(code,1) "\n  /* $t $name\[$length\] */\n"
  append C(code,1) "$b.type=S_STRUCT;\n"
  append C(code,1) "$b.d.p_struct.cname=\"$cntr\";\n"
  append C(code,1) "$b.d.s_struct.offset=C_OFFSET(_STRUCT_,$name);\n"
  append C(code,1) "$b.d.s_struct.length=$length;\n"
  append C(code,1) "$b.d.s_struct.size=sizeof($t);\n"
  incr C(blocks)
 }

 {^${W*}(unsigned${W+})?([a-zA-Z0-9_]+)${W+}([a-zA-Z0-9_]+)\[([0-9]+)\];${W*}/\*${W*}container:([a-zA-Z0-9_\./]+)${W*}} {
  # array of static typed stuctures
  global C
  set t $2
  set name $3
  set C(type,$name) $t 
  set C(member,$name) $C(blocks)
  set length $4
  set cntr $5
  set T [string toupper $t]
  set b "  c->desc\[$C(blocks)\]"
  append C(code,1) "\n  /* $t $name\[$length\] */\n"
  append C(code,1) "$b.type=S_STRUCT;\n"
  append C(code,1) "$b.d.p_struct.cname=\"$cntr\";\n"
  append C(code,1) "$b.d.s_struct.offset=C_OFFSET(_STRUCT_,$name\[0\]);\n"
  append C(code,1) "$b.d.s_struct.length=$length;\n"
  append C(code,1) "$b.d.s_struct.size=sizeof($t);\n"
  incr C(blocks)
 }

 {^${W*}(unsigned${W+})?((struct${W+})?[a-zA-Z0-9_]+)${W+}\*([a-zA-Z0-9_]+);${W*}/\*${W*}container:([a-zA-Z0-9_\./]+)${W+}len:([a-zA-Z0-9_]+)${W*}} {
  # array of dynamic typed stuctures
  # check (struct ...) above if strangeness occurs
  global C
  set t $2
  set name $4
  set C(type,$name) $t 
  set C(member,$name) $C(blocks)
  set cntr $5
  set index $6
  set T [string toupper $t]
  set b "  c->desc\[$C(blocks)\]"
  append C(code,1) "\n  /* $t $name\[$index\] */\n"
  append C(code,1) "$b.type=P_STRUCT;\n"
  append C(code,1) "$b.d.p_struct.offset=C_OFFSET(_STRUCT_,$name);\n"
  append C(code,1) "$b.d.p_struct.cname=\"$cntr\";\n"
  append C(code,1) "$b.d.p_struct.index=\$C(member,$index);\n"
  append C(code,1) "$b.d.p_struct.size=sizeof($t);\n"
  incr C(blocks)
 }
}]

proc Trace msg {
 upvar filename filename linenum linenum
 puts stderr "$filename,$linenum: $msg"
}

proc Line {file filename linenumber pattern {default {}} {eof {}}} {
# puts stderr [info level 0]
 upvar $linenumber linenum
 while {[gets $file line]>=0} {
  incr linenum
  set found 0
  foreach {exp code} $pattern {
   if [regexp $exp $line x 1 2 3 4 5 6 7 8 9] {
    set found 1
    eval $code
    break
   }
  }
  if !$found {eval $default}
 }
 eval $eof
 close $file
}


foreach f $argv {lappend sargv [file tail $f]}
puts $Outfile {/*
 * Copyright (C)1996-2000 Jacques H. de Villiers <Jacques@deVilliers.com>
 * Copyright (C)1996-2000 Center for Spoken Language Understanding,
 *                        Oregon Graduate Institute of Science and Technology
 *
 * See the file "license.ogi" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *}
puts $Outfile " * created by $argv0"
puts $Outfile " *         on [clock format [clock seconds]]"
puts $Outfile " *         from $sargv\n *"
puts $Outfile { *---------------------------------------------------------------------------
 */}

puts -nonewline $Outfile $Code(include)
foreach f $sargv {
 puts $Outfile "#include \"$f\""
}
puts -nonewline $Outfile $Code(head)
foreach f $argv {
 set in [open $f]
 set Out {}
 set Error 0
 set linenum 0
 Line $in $f linenum $Container
 if $Error {exit 2}
 puts -nonewline $Outfile $Out
}
puts -nonewline $Outfile $Code(tail)
close $Outfile
