Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add defaults to function definitions #20

Open
timm opened this issue Aug 14, 2019 · 1 comment
Open

add defaults to function definitions #20

timm opened this issue Aug 14, 2019 · 1 comment

Comments

@timm
Copy link
Owner

timm commented Aug 14, 2019

# vim: nospell filetype=awk ts=2 sw=2 sts=2  et :
BEGIN{
  demo("function  Aa(self ,c,d=1  ,e=\"tuesday\", aa= Anads, f, g  ) { # as")
  demo("function  aa(self) ")
  demo("function  Aa(self) ")
  demo("function  aa() { ")
  demo("function  Aa() ")
  demo("function  Aa(i,a=1,c=2) ")
  demo("function  aa(i,a=\"am do\",d=List, c=2) ")
}
function demo(s) {
  print("")
  print("in:\t["s"]")
  prep(s)
  print("fun:\t["PrepFun"]")
  print("s:\t["Preps"]")
}
function prep(s,   a,n,kw) {     # main function
  Preps = PrepFun = ""
	split(prepParts(s), a, ":::")   # main parts of the line
  n = prepKeywords(a[2], ks, vs) # get argumenet key values in ks vs
  PrepFun= prepMain(n,ks,vs,a[1],a[3],ks[1],a[1]~/^[A-Z]/)
}
function prepMain(n,       # number of args on command line
                  ks,vs,   # key,value for args. vs[i] empty if no default
                  f,       # function name
                  end,     # anything on the line after the args
                  self,    # first arguemnt to function
                  methodp, # true if function starts with uppercase
                  j,k,v,raw ){ # local vars
  for(j=1;j<=n;j++) {
    k = ks[j]
    v = vs[j]
    if (raw) {raw=raw","k} else {raw=k}
    if (v) {
      if (v ~ /^[A-Z]/) 
        Preps = Preps "has("self",\""k"\",\""v"\");"
      else
        Preps = Preps (methodp ? (self".") : "") \
               k"=("k"==\"\"?"v":"k"));" }}
  return "function " f "(" raw end
}

function prepParts(s,  pre,fun,end) {
  pre  = "^[ \t]*function[ \t]*"
  fun  = "[A-Za-z0-9_]+"
  end  = "{)#"
  return gensub(pre"("fun")\\(([^"end"]*)(["end"])",  
                "\\1:::\\2:::\\3","g",s)
}
function prepKeywords(s,ks,vs,  a,n,j,tmp) {
  # split arguments on ",", then split each arg on "="
  n = split(s,a,"[ \t]*,[\t ]*")
  for(j=1;j<=n;j++) {
    split(a[j], tmp, /[ \t]*=[ \t]*/)
    ks[j] = tmp[1]
    vs[j] = tmp[2] }
  return n
}
@timm
Copy link
Owner Author

timm commented Aug 14, 2019

and why not add subclassing here too? let constructors be

function kid<parent(self,

the add

  • ISA[kid]=parent
  • self["id"] = ++OID
  • self["aka"] = kid

then we can add polymorphism and method inheritance

aded runtime support

function my(i,f,    k,m) {
  k = i["isa"]
  while(k) {
    m=k f
    if (m in FUNCTAB) {MY=m; return ""}
    k=ISA[k]
  }
  print "E> failed method lookup on ["f"]"; 
  exit 1
}

pre-processor support

  In && /@[A-Z]/ {
  	$0= gensub(/@([A-Z].+)(\(([^,\)]+)[,\)])/, 
               "(my(\\3,\"\\1\"))@MY\\2","g",$0)
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant