Skip to content

Commit

Permalink
Revamp @define #140 : changing @define
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Sep 17, 2023
1 parent 87fa694 commit f7719ee
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 19 deletions.
33 changes: 22 additions & 11 deletions makesure.awk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ BEGIN {
delete GoalParams # name,paramI -> param name
delete CodePre # name -> pre-body (should also go before lib)
delete Code # name -> body
delete Vars # k -> "val"
delete DefineOverrides # k -> ""
DefinesCode = ""
delete Dependencies # name,i -> dep goal
Expand All @@ -32,7 +33,7 @@ BEGIN {
delete Lib # name -> code
delete UseLibLineNo# name -> line no.
delete GoalToLib # goal name -> lib name
delete Quotes # NF -> quote of field ("'"|"$"|"")
delete Quotes # NF -> quote of field ("'"|"$"|"u"|"\"")
Mode = "prelude" # prelude|define|goal|goal_glob|lib
srand()
prepareArgs()
Expand Down Expand Up @@ -123,7 +124,8 @@ function splitKV(arg, kv, n) {
}
function handleOptionDefineOverride(arg, kv) {
splitKV(arg, kv)
handleDefineLine(kv[0] "=" quoteArg(kv[1]))
# handleDefineLine(kv[0] "=" quoteArg(kv[1]))
Vars[kv[0]] = kv[1]
DefineOverrides[kv[0]]
}

Expand All @@ -142,8 +144,14 @@ function handleOptions( i) {

function handleDefine() {
started("define")
$1 = ""
handleDefineLine($0)
# $1 = ""
# handleDefineLine($0)
if (NF != 3) {
addError("@define NF != 3") # TODO better error msg
return
}
if (!($2 in DefineOverrides))
Vars[$2] = $3
}
function handleDefineLine(line, kv) {
if (!checkValidDefineSyntax(line))
Expand Down Expand Up @@ -366,9 +374,12 @@ function checkBeforeRun( i,j,dep,depCnt,goalName) {
}
}

function getPreludeCode( a) {
function getPreludeCode( a,k) {
addLine(a, MyDirScript)
addLine(a, DefinesCode)
# addLine(a, DefinesCode)
for (k in Vars) {
addLine(a, k "=" quoteArg(Vars[k]) ";export " k)
}
return a[0]
}

Expand Down Expand Up @@ -850,7 +861,7 @@ function parseCli_2(line, vars, res, pos,c,c1,isDoll,q,var,inDef,defVal,val,w,
if (inDef) {
if ("}" == c)
break
if ("\\" == c && ((c1 = substr(line,pos+1,1)) == "$" || c1 == "\\" || c1 == "}" || c1 == "\"")) {
if ("\\" == c && ((c1 = substr(line,pos + 1,1)) == "$" || c1 == "\\" || c1 == "}" || c1 == "\"")) {
c = c1; pos++
}
defVal = defVal c
Expand All @@ -868,7 +879,7 @@ function parseCli_2(line, vars, res, pos,c,c1,isDoll,q,var,inDef,defVal,val,w,
}
w = w c
}
res[i=+res[-7]++,"quote"] = isDoll ? "$" : q
res[i = +res[-7]++,"quote"] = isDoll ? "$" : q
res[i] = w
if ((c = substr(line,++pos,1)) != "" && c != " " && c != "\t")
return "joined arguments"
Expand All @@ -880,14 +891,14 @@ function parseCli_2(line, vars, res, pos,c,c1,isDoll,q,var,inDef,defVal,val,w,
}
if (w !~ /^[_A-Za-z0-9@.]+$/)
return "wrong unquoted: '" w "'"
res[i=+res[-7]++,"quote"] = "u"
res[i = +res[-7]++,"quote"] = "u"
res[i] = w
}
}
}
}
function reparseCli( todo,res,i,err) {
err = parseCli_2($0, todo, res)
function reparseCli( res,i,err) {
err = parseCli_2($0, Vars, res)
if (err) {
addError("Syntax error: " err)
die(Error)
Expand Down
6 changes: 3 additions & 3 deletions tests/10_define.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@


@define A=aaa
@define B="${A}bbb"
@define Commented=value # just a comment
@define A aaa
@define B "${A}bbb"
@define Commented value # just a comment

@goal testA
echo A=$A
Expand Down
2 changes: 1 addition & 1 deletion tests/10_define_in_goal.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

@goal default
@define A=1
@define A 1
echo "$0"
2 changes: 1 addition & 1 deletion tests/4_trace.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@define A=aaa
@define A aaa

@goal default
@reached_if [[ 1 -eq 2 ]]
Expand Down
2 changes: 1 addition & 1 deletion tests/4_trace_shell_sh.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@shell sh

@define A=aaa
@define A aaa

@goal default
@reached_if [ 1 -eq 2 ]
Expand Down
2 changes: 1 addition & 1 deletion tests/9_prelude.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# comment with whitespaces before it

@shell bash
@define A=B
@define A B

@goal a
@depends_on b
Expand Down
2 changes: 1 addition & 1 deletion tests/9_prelude_define_wrong2.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

@define A=aaa echo 'Hello'
@define A aaa echo 'Hello'

@goal default
echo "$0"

0 comments on commit f7719ee

Please sign in to comment.