Repository: goplus/gop
Pull request #1905 (2024-06-25 03:39:05)

c string literal; llgo v0.8.9


Patch:
diff --git a/printer/printer.go b/printer/printer.go
index 0c15852b..311c5f9d 100644
--- a/printer/printer.go
+++ b/printer/printer.go
@@ -313,7 +313,7 @@ func (p *printer) writeString(pos token.Position, s string, isLit bool) {
 		// UTF-8 sequences.
 		p.output = append(p.output, tabwriter.Escape)
 		if p.lastTok == token.CSTRING {
-			p.output = append(p.output, 'C')
+			p.output = append(p.output, 'c')
 		}
 	}
 
diff --git a/scanner/scanner.go b/scanner/scanner.go
index f84fa245..dbe0a845 100644
--- a/scanner/scanner.go
+++ b/scanner/scanner.go
@@ -838,7 +838,7 @@ scanAgain:
 			case token.IDENT, token.BREAK, token.CONTINUE, token.FALLTHROUGH, token.RETURN:
 				insertSemi = true
 			}
-		} else if lit == "C" && s.ch == '"' { // C"..."
+		} else if (lit == "C" || lit == "c") && s.ch == '"' { // C"..."
 			s.next()
 			insertSemi = true
 			tok = token.CSTRING
diff --git a/token/token.go b/token/token.go
index f13a2f22..358d3bdd 100644
--- a/token/token.go
+++ b/token/token.go
@@ -145,7 +145,7 @@ const (
 
 	ENV = additional_end2 // ${name}
 
-	CSTRING  = literal_beg  // C"Hello"
+	CSTRING  = literal_beg  // c"Hello"
 	RAT      = literal_end  // 123.5r
 	DRARROW  = operator_beg // => (double right arrow)
 	QUESTION = operator_end // ?


Test patch:
diff --git a/parser/_testdata/c2gohello/hello.gop b/parser/_testdata/c2gohello/hello.gop
index fb671ed3..d40ddf9c 100644
--- a/parser/_testdata/c2gohello/hello.gop
+++ b/parser/_testdata/c2gohello/hello.gop
@@ -1,3 +1,3 @@
 import "C"
 
-C.printf C"Hello, world!\n"
+C.printf c"Hello, world!\n"
diff --git a/testdata/_llgo/callpy/callpy.gop b/testdata/_llgo/callpy/callpy.gop
index 20f40471..b7759703 100644
--- a/testdata/_llgo/callpy/callpy.gop
+++ b/testdata/_llgo/callpy/callpy.gop
@@ -5,4 +5,4 @@ import (
 )
 
 x := math.sqrt(py.float(2))
-c.printf C"sqrt(2) = %f\n", x.float64
+c.printf c"sqrt(2) = %f\n", x.float64
diff --git a/testdata/_llgo/defer/defer.gop b/testdata/_llgo/defer/defer.gop
index ad3d27bb..f75767d1 100644
--- a/testdata/_llgo/defer/defer.gop
+++ b/testdata/_llgo/defer/defer.gop
@@ -5,11 +5,11 @@ func f(s string) bool {
 }
 
 defer func() {
-	c.printf C"hi\n"
+	c.printf c"hi\n"
 }()
 if s := "hello"; f(s) {
-	defer c.printf(C"%s\n", c.allocaCStr(s))
+	defer c.printf(c"%s\n", c.allocaCStr(s))
 } else {
-	defer c.printf(C"world\n")
+	defer c.printf(c"world\n")
 }
-defer c.printf(C"bye\n")
+defer c.printf(c"bye\n")
diff --git a/testdata/_llgo/goroutine/goroutine.gop b/testdata/_llgo/goroutine/goroutine.gop
index c34cf6c3..2c9633cc 100644
--- a/testdata/_llgo/goroutine/goroutine.gop
+++ b/testdata/_llgo/goroutine/goroutine.gop
@@ -2,9 +2,9 @@ import "c"
 
 done := false
 go func() {
-	c.printf C"Hello, goroutine\n"
+	c.printf c"Hello, goroutine\n"
 	done = true
 }()
 for !done {
-	c.printf C"."
+	c.printf c"."
 }
diff --git a/testdata/_llgo/hello/hello.gop b/testdata/_llgo/hello/hello.gop
index 971dd677..3ea999eb 100644
--- a/testdata/_llgo/hello/hello.gop
+++ b/testdata/_llgo/hello/hello.gop
@@ -1,3 +1,3 @@
 import "c"
 
-c.printf C"Hello world\n"
+c.printf c"Hello world\n"
diff --git a/testdata/_llgo/hellollgo/hello.gop b/testdata/_llgo/hellollgo/hello.gop
index 6c5e8d59..4cd048eb 100644
--- a/testdata/_llgo/hellollgo/hello.gop
+++ b/testdata/_llgo/hellollgo/hello.gop
@@ -1,4 +1,4 @@
 import "c"
 
-c.printf C"Hello, llgo!\n"
-c.fprintf c.Stderr, C"Hi, %6.1f\n", 3.14
+c.printf c"Hello, llgo!\n"
+c.fprintf c.Stderr, c"Hi, %6.1f\n", 3.14
diff --git a/testdata/_llgo/matrix/matrix.gop b/testdata/_llgo/matrix/matrix.gop
index b16237f4..ad4fb3fa 100644
--- a/testdata/_llgo/matrix/matrix.gop
+++ b/testdata/_llgo/matrix/matrix.gop
@@ -15,4 +15,4 @@ b := py.list(
 	py.list(3.0, 2.0, 1.0),
 )
 x := numpy.add(a, b)
-c.printf C"a+b = %s\n", x.str.cStr
+c.printf c"a+b = %s\n", x.str.cStr
diff --git a/testdata/_llgo/qsort/qsort.gop b/testdata/_llgo/qsort/qsort.gop
index 3bad6e22..0203fd26 100644
--- a/testdata/_llgo/qsort/qsort.gop
+++ b/testdata/_llgo/qsort/qsort.gop
@@ -8,5 +8,5 @@ c.qsort unsafe.Pointer(&a[0]), 5, unsafe.Sizeof(0), (a, b) => {
 	return c.Int(*(*int)(a) - *(*int)(b))
 }
 for v <- a {
-	c.printf C"%d\n", v
+	c.printf c"%d\n", v
 }
diff --git a/testdata/_llgo/sqlitedemo/sqlitedemo.gop b/testdata/_llgo/sqlitedemo/sqlitedemo.gop
index 14ad1116..46b62094 100644
--- a/testdata/_llgo/sqlitedemo/sqlitedemo.gop
+++ b/testdata/_llgo/sqlitedemo/sqlitedemo.gop
@@ -1,57 +1,58 @@
 import (
 	"c"
 	"c/sqlite"
+	"c/os"
 )
 
 func check(err sqlite.Errno, db *sqlite.Sqlite3, at string) {
 	if err != sqlite.OK {
-		c.Printf(c.Str("==> %s Error: (%d) %s\n"), c.AllocaCStr(at), err, db.Errmsg())
-		c.Exit(1)
+		c.printf c"==> %s Error: (%d) %s\n", c.allocaCStr(at), err, db.errmsg
+		c.exit 1
 	}
 }
 
 func checkDone(err sqlite.Errno, db *sqlite.Sqlite3, at string) {
 	if err != sqlite.Done {
-		check(err, db, at)
+		check err, db, at
 	}
 }
 
-c.Remove(c.Str("test.db"))
+os.remove c"test.db"
 
-db, err := sqlite.Open(c.Str("test.db"))
-check(err, db, "sqlite: Open")
+db, err := sqlite.open(c"test.db")
+check err, db, "sqlite: Open"
 
-err = db.Exec(c.Str("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)"), nil, nil, nil)
-check(err, db, "sqlite: Exec CREATE TABLE")
+err = db.exec(c"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)", nil, nil, nil)
+check err, db, "sqlite: Exec CREATE TABLE"
 
-stmt, err := db.PrepareV3("INSERT INTO users (id, name) VALUES (?, ?)", 0, nil)
-check(err, db, "sqlite: PrepareV3 INSERT")
+stmt, err := db.prepareV3("INSERT INTO users (id, name) VALUES (?, ?)", 0, nil)
+check err, db, "sqlite: PrepareV3 INSERT"
 
-stmt.BindInt(1, 100)
-stmt.BindText(2, c.Str("Hello World"), -1, nil)
+stmt.bindInt 1, 100
+stmt.bindText 2, c"Hello World", -1, nil
 
-err = stmt.Step()
-checkDone(err, db, "sqlite: Step INSERT 1")
+err = stmt.step
+checkDone err, db, "sqlite: Step INSERT 1"
 
-stmt.Reset()
-stmt.BindInt(1, 200)
-stmt.BindText(2, c.Str("This is llgo"), -1, nil)
+stmt.reset
+stmt.bindInt 1, 200
+stmt.bindText 2, c"This is llgo", -1, nil
 
-err = stmt.Step()
-checkDone(err, db, "sqlite: Step INSERT 2")
+err = stmt.step
+checkDone err, db, "sqlite: Step INSERT 2"
 
-stmt.Close()
+stmt.close
 
-stmt, err = db.PrepareV3("SELECT * FROM users", 0, nil)
-check(err, db, "sqlite: PrepareV3 SELECT")
+stmt, err = db.prepareV3("SELECT * FROM users", 0, nil)
+check err, db, "sqlite: PrepareV3 SELECT"
 
 for {
-	if err = stmt.Step(); err != sqlite.HasRow {
+	if err = stmt.step; err != sqlite.HasRow {
 		break
 	}
-	c.Printf(c.Str("==> id=%d, name=%s\n"), stmt.ColumnInt(0), stmt.ColumnText(1))
+	c.printf c"==> id=%d, name=%s\n", stmt.columnInt(0), stmt.columnText(1)
 }
-checkDone(err, db, "sqlite: Step done")
+checkDone err, db, "sqlite: Step done"
 
-stmt.Close()
-db.Close()
+stmt.close
+db.close
diff --git a/testdata/_llgo/statistics/statistics.gop b/testdata/_llgo/statistics/statistics.gop
index 072bd06b..5e2e1950 100644
--- a/testdata/_llgo/statistics/statistics.gop
+++ b/testdata/_llgo/statistics/statistics.gop
@@ -6,4 +6,4 @@ import (
 
 list := py.list(1.0, 2.0, 3.0, 4.0, 4.0)
 mean := statistics.mean(list)
-c.printf(C"mean(1, 2, 3, 4, 4) = %f\n", mean.float64)
+c.printf c"mean(1, 2, 3, 4, 4) = %f\n", mean.float64