Best Ginkgo code snippet using types.LineNumber
operations.go
Source:operations.go
...8 if err != nil {9 return "", nil, err10 }11 if len(rts) != 1 {12 return "", nil, msg(o.LineNumber, o.Column, "'make' operation has improper second operand.")13 }14 if !isType(rts[0], BuiltinType{"I", nil}, true) {15 return "", nil, msg(o.LineNumber, o.Column, "'make' operation requires at least two operands.")16 }17 if len(o.MakeType.Params) != 1 {18 return "", nil, msg(o.LineNumber, o.Column, "'make' operation requires a slice or list type")19 }20 dt, err := getDataType(o.MakeType.Params[0], pkg)21 if err != nil {22 return "", nil, err23 }24 typeStr, err := compileType(dt, pkg)25 if err != nil {26 return "", nil, err27 }28 switch o.MakeType.Type {29 case "S":30 code := "(make([]" + typeStr + "," + numStr + "))"31 return code, []DataType{BuiltinType{"S", []DataType{dt}}}, nil32 case "L":33 code := "(func () *_std.List {\n"34 code += "var _list _std.List = make([]interface{}, " + numStr + ")\n"35 code += `return &_list36 })()`37 return code, []DataType{BuiltinType{"S", []DataType{dt}}}, nil38 default:39 return "", nil, msg(o.LineNumber, o.Column, "'make' operation requires a slice or list type")40 }41}42func compileOperation(o Operation, pkg *Package, locals map[string]Variable) (string, []DataType, error) {43 if o.Operator == "make" {44 return compileMakeOp(o, pkg, locals)45 }46 operandCode := make([]string, len(o.Operands))47 operandTypes := make([]DataType, len(o.Operands))48 for i, expr := range o.Operands {49 if i == 1 {50 if o.Operator == "get" {51 switch st := operandTypes[0].(type) {52 case Struct:53 switch token := expr.(type) {54 case Token:55 if token.Type == IdentifierWord {56 returnType, err := st.getMemberType(token.Content)57 if err != nil {58 return "", nil, err59 }60 return operandCode[0] + "." + strings.Title(token.Content),61 []DataType{returnType}, nil62 }63 }64 }65 } else if o.Operator == "set" {66 if len(o.Operands) != 3 {67 return "", nil, msg(o.LineNumber, o.Column, "'set' operation requires 3 operands")68 }69 switch st := operandTypes[0].(type) {70 case Struct:71 switch token := expr.(type) {72 case Token:73 if token.Type == IdentifierWord {74 returnType, err := st.getMemberType(token.Content)75 if err != nil {76 return "", nil, err77 }78 val, valTypes, err := compileExpression(o.Operands[2], pkg, locals)79 if err != nil {80 return "", nil, err81 }82 if len(valTypes) != 1 {83 return "", nil, msg(o.LineNumber, o.Column, "'set' operation value expression should return just one value")84 }85 if !isType(valTypes[0], returnType, false) {86 return "", nil, msg(o.LineNumber, o.Column, "'set' operation value expression has wrong type for the target struct field")87 }88 rt, err := compileType(returnType, pkg)89 if err != nil {90 return "", nil, err91 }92 return "(func () " + rt + " { _t := " + val + "; " + operandCode[0] +93 "." + strings.Title(token.Content) + " = _t; return _t }())",94 []DataType{returnType}, nil95 }96 }97 }98 }99 }100 c, returnTypes, err := compileExpression(expr, pkg, locals)101 if err != nil {102 return "", nil, err103 }104 if len(returnTypes) != 1 {105 return "", nil, msg(o.LineNumber, o.Column, "operand expression returns more than one value.")106 }107 operandCode[i] = c108 operandTypes[i] = returnTypes[0]109 }110 code := "("111 var returnType DataType112 switch o.Operator {113 case "add":114 if len(o.Operands) < 2 {115 return "", nil, msg(o.LineNumber, o.Column, "'add' operations requires at least two operands.")116 }117 t := operandTypes[0]118 if !isNumber(t) {119 return "", nil, msg(o.LineNumber, o.Column, "'add' operation has non-number operand")120 }121 for i := range o.Operands {122 if !isType(operandTypes[i], t, true) {123 return "", nil, msg(o.LineNumber, o.Column, "'add' operation has operand whose type differs from the others")124 }125 code += operandCode[i]126 if i < len(o.Operands)-1 {127 code += " + "128 }129 }130 returnType = t131 case "sub":132 if len(o.Operands) < 2 {133 return "", nil, msg(o.LineNumber, o.Column, "'sub' operation requires at least two operands")134 }135 t := operandTypes[0]136 if !isNumber(t) {137 return "", nil, msg(o.LineNumber, o.Column, "'sub' operation has non-number operand")138 }139 for i := range o.Operands {140 if !isType(operandTypes[i], t, true) {141 return "", nil, msg(o.LineNumber, o.Column, "'sub' operation has operand whose type differs from the others")142 }143 code += operandCode[i]144 if i < len(o.Operands)-1 {145 code += " - "146 }147 }148 returnType = t149 case "mul":150 if len(o.Operands) < 2 {151 return "", nil, msg(o.LineNumber, o.Column, "mul operation requires at least two operands")152 }153 t := operandTypes[0]154 if !isNumber(t) {155 return "", nil, msg(o.LineNumber, o.Column, "mul operation has non-number operand")156 }157 for i := range o.Operands {158 if !isType(operandTypes[i], t, true) {159 return "", nil, msg(o.LineNumber, o.Column, "mul operation has non-number operand")160 }161 code += operandCode[i]162 if i < len(o.Operands)-1 {163 code += " * "164 }165 }166 returnType = t167 case "div":168 if len(o.Operands) < 2 {169 return "", nil, msg(o.LineNumber, o.Column, "div operation requires at least two operands")170 }171 t := operandTypes[0]172 if !isNumber(t) {173 return "", nil, msg(o.LineNumber, o.Column, "div operation has non-number operand")174 }175 for i := range o.Operands {176 if !isType(operandTypes[i], t, true) {177 return "", nil, msg(o.LineNumber, o.Column, "div operation has non-number operand")178 }179 code += operandCode[i]180 if i < len(o.Operands)-1 {181 code += " / "182 }183 }184 returnType = t185 case "inc":186 if len(o.Operands) != 1 {187 return "", nil, msg(o.LineNumber, o.Column, "inc operation requires one operand.")188 }189 t := operandTypes[0]190 if !isNumber(t) {191 return "", nil, msg(o.LineNumber, o.Column, "inc operation has non-number operand")192 }193 code += operandCode[0] + " + 1"194 returnType = t195 case "dec":196 if len(o.Operands) != 1 {197 return "", nil, msg(o.LineNumber, o.Column, "dec operation requires one operand.")198 }199 t := operandTypes[0]200 if !isNumber(t) {201 return "", nil, msg(o.LineNumber, o.Column, "dec operation has non-number operand")202 }203 code += operandCode[0] + " - 1"204 returnType = t205 case "mod":206 if len(o.Operands) != 2 {207 return "", nil, msg(o.LineNumber, o.Column, "mod operation requires two operands")208 }209 t := operandTypes[0]210 if !isNumber(t) {211 return "", nil, msg(o.LineNumber, o.Column, "mod operation has non-number operand")212 }213 for i := range o.Operands {214 if !isType(operandTypes[i], t, true) {215 return "", nil, msg(o.LineNumber, o.Column, "mod operation has non-number operand")216 }217 code += "int64(" + operandCode[i] + ")"218 if i < len(o.Operands)-1 {219 code += " % "220 }221 }222 returnType = t223 case "eq":224 if len(o.Operands) < 2 {225 return "", nil, msg(o.LineNumber, o.Column, "eq operation requires at least two operands")226 }227 returnType = BuiltinType{"Bool", nil}228 for i := 0; i < len(o.Operands)-1; i++ {229 if !isType(operandTypes[i], operandTypes[0], true) ||230 !isType(operandTypes[i+1], operandTypes[0], true) {231 return "", nil, msg(o.LineNumber, o.Column, "eq operation has mismatched operand types")232 }233 if i > 0 {234 code += " && "235 }236 code += operandCode[i] + " == " + operandCode[i+1]237 }238 case "neq":239 if len(o.Operands) < 2 {240 return "", nil, msg(o.LineNumber, o.Column, "neq operation requires at least two operands")241 }242 returnType = BuiltinType{"Bool", nil}243 for i := 0; i < len(o.Operands)-1; i++ {244 if !isType(operandTypes[i], operandTypes[0], true) ||245 !isType(operandTypes[i+1], operandTypes[0], true) {246 return "", nil, msg(o.LineNumber, o.Column, "neq operation has mismatched operand types")247 }248 if i > 0 {249 code += " && "250 }251 code += operandCode[i] + " != " + operandCode[i+1]252 }253 case "not":254 if len(o.Operands) != 1 {255 return "", nil, msg(o.LineNumber, o.Column, "not operation requires one operand")256 }257 returnType = BuiltinType{"Bool", nil}258 if !isType(operandTypes[0], returnType, true) {259 return "", nil, msg(o.LineNumber, o.Column, "not operation has a non-bool operand")260 }261 code += "!" + operandCode[0]262 case "lt":263 if len(o.Operands) < 2 {264 return "", nil, msg(o.LineNumber, o.Column, "lt operation requires at least two operands")265 }266 returnType = BuiltinType{"Bool", nil}267 t := operandTypes[0]268 if !isNumber(t) {269 return "", nil, msg(o.LineNumber, o.Column, "lt operation has non-number operand")270 }271 for i := 0; i < len(o.Operands)-1; i++ {272 if !isType(operandTypes[i], t, true) ||273 !isType(operandTypes[i+1], t, true) {274 return "", nil, msg(o.LineNumber, o.Column, "lt operation has non-number operand")275 }276 if i > 0 {277 code += " && "278 }279 code += operandCode[i] + " < " + operandCode[i+1]280 }281 case "gt":282 if len(o.Operands) < 2 {283 return "", nil, msg(o.LineNumber, o.Column, "gt operation requires at least two operands")284 }285 returnType = BuiltinType{"Bool", nil}286 t := operandTypes[0]287 if !isNumber(t) {288 return "", nil, msg(o.LineNumber, o.Column, "lt operation has non-number operand")289 }290 for i := 0; i < len(o.Operands)-1; i++ {291 if !isType(operandTypes[i], t, true) ||292 !isType(operandTypes[i+1], t, true) {293 return "", nil, msg(o.LineNumber, o.Column, "gt operation has non-number operand")294 }295 if i > 0 {296 code += " && "297 }298 code += operandCode[i] + " > " + operandCode[i+1]299 }300 case "lte":301 if len(o.Operands) < 2 {302 return "", nil, msg(o.LineNumber, o.Column, "lte operation requires at least two operands")303 }304 returnType = BuiltinType{"Bool", nil}305 t := operandTypes[0]306 if !isNumber(t) {307 return "", nil, msg(o.LineNumber, o.Column, "lt operation has non-number operand")308 }309 for i := 0; i < len(o.Operands)-1; i++ {310 if !isType(operandTypes[i], t, true) ||311 !isType(operandTypes[i+1], t, true) {312 return "", nil, msg(o.LineNumber, o.Column, "lte operation has non-number operand")313 }314 if i > 0 {315 code += " && "316 }317 code += operandCode[i] + " <= " + operandCode[i+1]318 }319 case "gte":320 if len(o.Operands) < 2 {321 return "", nil, msg(o.LineNumber, o.Column, "gte operation requires at least two operands")322 }323 returnType = BuiltinType{"Bool", nil}324 t := operandTypes[0]325 if !isNumber(t) {326 return "", nil, msg(o.LineNumber, o.Column, "lt operation has non-number operand")327 }328 for i := 0; i < len(o.Operands)-1; i++ {329 if !isType(operandTypes[i], t, true) ||330 !isType(operandTypes[i+1], t, true) {331 return "", nil, msg(o.LineNumber, o.Column, "gte operation has non-number operand")332 }333 if i > 0 {334 code += " && "335 }336 code += operandCode[i] + " >= " + operandCode[i+1]337 }338 case "get":339 if len(o.Operands) < 2 {340 return "", nil, msg(o.LineNumber, o.Column, "get operation has too few operands")341 }342 switch t := operandTypes[0].(type) {343 case BuiltinType:344 switch t.Name {345 case "M":346 returnType = t.Params[1]347 if !isType(operandTypes[1], t.Params[0], true) {348 return "", nil, msg(o.LineNumber, o.Column, "get operation on map has wrong type as second operand")349 }350 code += operandCode[0] + "[" + operandCode[1] + "]"351 case "L", "S":352 returnType = t.Params[0]353 dt, err := compileType(returnType, pkg)354 if err != nil {355 return "", nil, err356 }357 if !isNumber(operandTypes[1]) {358 return "", nil, msg(o.LineNumber, o.Column, "get operation on list or slice requires a number as second operand")359 }360 if t.Name == "L" {361 code += "(*"362 } else if t.Name == "S" {363 code += "("364 }365 code += operandCode[0] + ")[int64(" + operandCode[1] + ")]"366 if t.Name == "L" && o.Operator == "get" {367 code += ".(" + dt + ")"368 }369 default:370 return "", nil, msg(o.LineNumber, o.Column, "get operation requires a list or map as first operand.")371 }372 case ArrayType:373 returnType = t.Type374 if !isNumber(operandTypes[1]) {375 return "", nil, msg(o.LineNumber, o.Column, "get operation on an array requires a number as second operand")376 }377 code += operandCode[0] + "[int64(" + operandCode[1] + ")]"378 default:379 return "", nil, msg(o.LineNumber, o.Column, "get operation requires a list or map as first operand.")380 }381 case "set":382 if len(o.Operands) != 3 {383 return "", nil, msg(o.LineNumber, o.Column, "set operation requires three operands")384 }385 switch t := operandTypes[0].(type) {386 case BuiltinType:387 switch t.Name {388 case "M":389 if !isType(operandTypes[1], t.Params[0], true) {390 return "", nil, msg(o.LineNumber, o.Column, "set operation on map has wrong type as second operand")391 }392 if !isType(operandTypes[2], t.Params[1], false) {393 return "", nil, msg(o.LineNumber, o.Column, "set operation on map has wrong type as third operand")394 }395 code += "func () {" + operandCode[0] + "[" + operandCode[1] + "] = " + operandCode[2] + "}()"396 case "L":397 if !isNumber(operandTypes[1]) {398 return "", nil, msg(o.LineNumber, o.Column, "set operation requires a number as second operand")399 }400 if !isType(operandTypes[2], t.Params[0], false) {401 return "", nil, msg(o.LineNumber, o.Column, "set operation on list has wrong type as third operand")402 }403 code += operandCode[0] + ".Set(int64(" + operandCode[1] + "), " + operandCode[2] + ")"404 case "S":405 if !isNumber(operandTypes[1]) {406 return "", nil, msg(o.LineNumber, o.Column, "set operation requires a number as second operand")407 }408 if !isType(operandTypes[2], t.Params[0], false) {409 return "", nil, msg(o.LineNumber, o.Column, "set operation on list has wrong type as third operand")410 }411 code += "func () {" + operandCode[0] + "[" + operandCode[1] + "] = " + operandCode[2] + "}()"412 }413 case ArrayType:414 if !isNumber(operandTypes[1]) {415 return "", nil, msg(o.LineNumber, o.Column, "set operation requires a number as second operand")416 }417 if !isType(operandTypes[2], t.Type, false) {418 return "", nil, msg(o.LineNumber, o.Column, "set operation on list has wrong type as third operand")419 }420 code += "func () {" + operandCode[0] + "[" + operandCode[1] + "] = " + operandCode[2] + "}()"421 default:422 return "", nil, msg(o.LineNumber, o.Column, "set operation requires a list, map, slice, or array as first operand")423 }424 returnType = nil425 case "push":426 if len(o.Operands) != 2 {427 return "", nil, msg(o.LineNumber, o.Column, "push operation requires two operands")428 }429 switch t := operandTypes[0].(type) {430 case BuiltinType:431 if t.Name != "L" {432 return "", nil, msg(o.LineNumber, o.Column, "push operation's first operand must be a list.")433 }434 if !isType(operandTypes[1], t.Params[0], false) {435 return "", nil, msg(o.LineNumber, o.Column, "push operation's second operand is not valid for the list.")436 }437 code += operandCode[0] + ".Append(" + operandCode[1] + ")"438 default:439 return "", nil, msg(o.LineNumber, o.Column, "push operation requires first operand to be a list.")440 }441 returnType = nil442 case "append":443 if len(o.Operands) != 2 {444 return "", nil, msg(o.LineNumber, o.Column, "append operation requires two operands")445 }446 switch t := operandTypes[0].(type) {447 case BuiltinType:448 if t.Name != "S" {449 return "", nil, msg(o.LineNumber, o.Column, "append operation's first operand must be a slice.")450 }451 if !isType(operandTypes[1], t.Params[0], false) {452 return "", nil, msg(o.LineNumber, o.Column, "append operation's second operand is not valid for the slice.")453 }454 code += "append(" + operandCode[0] + ", " + operandCode[1] + ")"455 default:456 return "", nil, msg(o.LineNumber, o.Column, "append operation requires first operand to be a slice.")457 }458 returnType = operandTypes[0]459 case "slice":460 if len(o.Operands) != 3 {461 return "", nil, msg(o.LineNumber, o.Column, "'slice' operation requires three operands")462 }463 if !isNumber(operandTypes[1]) || !isNumber(operandTypes[2]) {464 return "", nil, msg(o.LineNumber, o.Column, "'slice' operation's second and third operands must be numbers.")465 }466 switch t := operandTypes[0].(type) {467 case BuiltinType:468 switch t.Name {469 case "Str":470 returnType = BuiltinType{"S", []DataType{BuiltinType{"Str", nil}}}471 case "S":472 returnType = operandTypes[0]473 default:474 return "", nil, msg(o.LineNumber, o.Column, "'slice' operation's first operand must be a slice or string.")475 }476 case ArrayType:477 returnType = BuiltinType{"S", []DataType{t.Type}}478 default:479 return "", nil, msg(o.LineNumber, o.Column, "'slice' operation requires first operand to be a slice.")480 }481 code += operandCode[0] + "[int64(" + operandCode[1] + "):int64(" + operandCode[2] + ")]"482 case "or":483 if len(o.Operands) < 2 {484 return "", nil, msg(o.LineNumber, o.Column, "or operation requires at least two operands")485 }486 returnType = BuiltinType{"Bool", nil}487 for i := range o.Operands {488 if !isType(operandTypes[i], returnType, true) {489 return "", nil, msg(o.LineNumber, o.Column, "or operation has non-boolean operand")490 }491 code += operandCode[i]492 if i < len(o.Operands)-1 {493 code += " || "494 }495 }496 case "and":497 if len(o.Operands) < 2 {498 return "", nil, msg(o.LineNumber, o.Column, "and operation requires at least two operands")499 }500 returnType = BuiltinType{"Bool", nil}501 for i := range o.Operands {502 if !isType(operandTypes[i], returnType, true) {503 return "", nil, msg(o.LineNumber, o.Column, "and operation has non-boolean operand")504 }505 code += operandCode[i]506 if i < len(o.Operands)-1 {507 code += " && "508 }509 }510 case "ref":511 if len(o.Operands) != 1 {512 return "", nil, msg(o.LineNumber, o.Column, "ref operation requires a single operand.")513 }514 switch e := o.Operands[0].(type) {515 case Token:516 switch e.Type {517 case IdentifierWord:518 name := e.Content519 if v, ok := locals[name]; ok {520 rt, err := getDataType(v.Type, pkg)521 if err != nil {522 return "", nil, err523 }524 returnType = BuiltinType{"P", []DataType{rt}}525 code += "&" + name526 } else if v, ok := pkg.Globals[name]; ok {527 code += "&G_" + name528 rt, err := getDataType(v.Type, pkg)529 if err != nil {530 return "", nil, err531 }532 returnType = BuiltinType{"P", []DataType{rt}}533 } else {534 return "", nil, msg(e.LineNumber, e.Column, "Name is undefined: "+name)535 }536 default:537 return "", nil, msg(o.LineNumber, o.Column, "ref operation has improper operand.")538 }539 case Operation:540 if e.Operator != "get" {541 return "", nil, msg(o.LineNumber, o.Column, "ref operation has improper operand.")542 }543 if len(o.Operands) != 2 {544 return "", nil, msg(o.LineNumber, o.Column, "get operation requires two operands")545 }546 t, ok := operandTypes[0].(BuiltinType)547 if !ok || (t.Name != "L" && t.Name != "M") {548 return "", nil, msg(o.LineNumber, o.Column, "get operation requires a list or map as first operand")549 }550 switch t.Name {551 case "M":552 returnType = t.Params[1]553 if !isType(operandTypes[1], t.Params[0], true) {554 return "", nil, msg(o.LineNumber, o.Column, "get operation on map has wrong type as second operand")555 }556 code += "&" + operandCode[0] + "[" + operandCode[1] + "]"557 case "L":558 returnType = t.Params[0]559 if !isNumber(operandTypes[1]) {560 return "", nil, msg(o.LineNumber, o.Column, "get operation requires a number as second operand")561 }562 code += "&(*" + operandCode[0] + ")[int64(" + operandCode[1] + ")]"563 }564 default:565 return "", nil, msg(o.LineNumber, o.Column, "ref operation requires a single operand.")566 }567 case "dr":568 if len(o.Operands) != 1 {569 return "", nil, msg(o.LineNumber, o.Column, "dr operation requires a single operand.")570 }571 dt, ok := operandTypes[0].(BuiltinType)572 if !ok && dt.Name != "P" {573 return "", nil, msg(o.LineNumber, o.Column, "dr operation requires a pointer operand.")574 }575 returnType = dt.Params[0]576 code += "*" + operandCode[0]577 case "band":578 if len(o.Operands) != 2 {579 return "", nil, msg(o.LineNumber, o.Column, "'band' operation requires two operands")580 }581 if !isNumber(operandTypes[0]) || !isNumber(operandTypes[1]) {582 return "", nil, msg(o.LineNumber, o.Column, "'band' operation requires two number operands")583 }584 code += operandCode[0] + " & " + operandCode[1]585 case "bor":586 if len(o.Operands) != 2 {587 return "", nil, msg(o.LineNumber, o.Column, "'bor' operation requires two operands")588 }589 if !isNumber(operandTypes[0]) || !isNumber(operandTypes[1]) {590 return "", nil, msg(o.LineNumber, o.Column, "'bor' operation requires two number operands")591 }592 code += operandCode[0] + " | " + operandCode[1]593 case "bxor":594 if len(o.Operands) != 2 {595 return "", nil, msg(o.LineNumber, o.Column, "'bxor' operation requires two operands")596 }597 if !isNumber(operandTypes[0]) || !isNumber(operandTypes[1]) {598 return "", nil, msg(o.LineNumber, o.Column, "'bxor' operation requires two number operands")599 }600 code += operandCode[0] + " ^ " + operandCode[1]601 case "bnot":602 if len(o.Operands) != 1 {603 return "", nil, msg(o.LineNumber, o.Column, "'bnot' operation requires one operand")604 }605 if !isNumber(operandTypes[0]) {606 return "", nil, msg(o.LineNumber, o.Column, "'bnot' operation requires one number operand")607 }608 code += "^" + operandCode[1]609 case "print":610 if len(o.Operands) < 1 {611 return "", nil, msg(o.LineNumber, o.Column, "'print' operation requires at least one operand")612 }613 code += "_fmt.Print("614 for i := range o.Operands {615 code += operandCode[i] + ", "616 }617 code += ")"618 case "println":619 if len(o.Operands) < 1 {620 return "", nil, msg(o.LineNumber, o.Column, "'println' operation requires at least one operand")621 }622 code += "_fmt.Println("623 for i := range o.Operands {624 code += operandCode[i] + ", "625 }626 code += ")"627 case "prompt":628 returnType = BuiltinType{"Str", nil}629 code += "_std.Prompt("630 for i := range o.Operands {631 code += operandCode[i] + ", "632 }633 code += ")"634 case "concat":635 if len(o.Operands) < 2 {636 return "", nil, msg(o.LineNumber, o.Column, "concat operation requires at least two operands")637 }638 returnType = BuiltinType{"Str", nil}639 for i := range o.Operands {640 if !isType(operandTypes[i], returnType, true) {641 return "", nil, msg(o.LineNumber, o.Column, "concat operation has non-string operand")642 }643 code += operandCode[i]644 if i < len(o.Operands)-1 {645 code += " + "646 }647 }648 case "getchar":649 if len(o.Operands) != 2 {650 return "", nil, msg(o.LineNumber, o.Column, "getchar operation requires two operands")651 }652 returnType = BuiltinType{"Str", nil}653 if !isType(operandTypes[0], returnType, true) {654 return "", nil, msg(o.LineNumber, o.Column, "getchar's first operand must be a string")655 }656 if !isInteger(operandTypes[1]) {657 return "", nil, msg(o.LineNumber, o.Column, "getchar's second operand must be an integer or byte")658 }659 code += "string(" + operandCode[0] + "[" + operandCode[1] + "])"660 case "getrune":661 if len(o.Operands) != 2 {662 return "", nil, msg(o.LineNumber, o.Column, "getchar operation requires two operands")663 }664 returnType = BuiltinType{"I", nil}665 if !isType(operandTypes[0], returnType, true) {666 return "", nil, msg(o.LineNumber, o.Column, "getchar's first operand must be a string")667 }668 if !isInteger(operandTypes[1]) {669 return "", nil, msg(o.LineNumber, o.Column, "getchar's second operand must be an integer or byte")670 }671 code += operandCode[0] + "[" + operandCode[1] + "]"672 case "charlist":673 if len(o.Operands) != 1 {674 return "", nil, msg(o.LineNumber, o.Column, "charlist operation requires one operand")675 }676 returnType = BuiltinType{"L", []DataType{BuiltinType{"Str", nil}}}677 if !isType(operandTypes[0], BuiltinType{"Str", nil}, true) {678 return "", nil, msg(o.LineNumber, o.Column, "charlist operand must be a string")679 }680 code += "_std.Charlist(" + operandCode[0] + ")"681 case "runelist":682 if len(o.Operands) != 1 {683 return "", nil, msg(o.LineNumber, o.Column, "runelist operation requires one operand")684 }685 returnType = BuiltinType{"L", []DataType{BuiltinType{"I", nil}}}686 if !isType(operandTypes[0], BuiltinType{"Str", nil}, true) {687 return "", nil, msg(o.LineNumber, o.Column, "runelist operand must be a string")688 }689 code += "_std.Runelist(" + operandCode[0] + ")"690 case "charslice":691 if len(o.Operands) != 1 {692 return "", nil, msg(o.LineNumber, o.Column, "charslice operation requires one operand")693 }694 returnType = BuiltinType{"S", []DataType{BuiltinType{"Str", nil}}}695 if !isType(operandTypes[0], BuiltinType{"Str", nil}, true) {696 return "", nil, msg(o.LineNumber, o.Column, "charslice operand must be a string")697 }698 code += "_std.Charslice(" + operandCode[0] + ")"699 case "runeslice":700 if len(o.Operands) != 1 {701 return "", nil, msg(o.LineNumber, o.Column, "runeslice operation requires one operand")702 }703 returnType = BuiltinType{"S", []DataType{BuiltinType{"I", nil}}}704 if !isType(operandTypes[0], BuiltinType{"Str", nil}, true) {705 return "", nil, msg(o.LineNumber, o.Column, "runeslice operand must be a string")706 }707 code += "_std.Runeslice(" + operandCode[0] + ")"708 case "byteslice":709 if len(o.Operands) != 1 {710 return "", nil, msg(o.LineNumber, o.Column, "'byteslice' operation requires one string operand")711 }712 returnType = BuiltinType{"S", []DataType{BuiltinType{"Byte", nil}}}713 if !isType(operandTypes[0], BuiltinType{"Str", nil}, true) {714 return "", nil, msg(o.LineNumber, o.Column, "'byteslice' operand must be a string")715 }716 code += "[]byte(" + operandCode[0] + ")"717 case "len":718 if len(o.Operands) != 1 {719 return "", nil, msg(o.LineNumber, o.Column, "len operation requires one operand")720 }721 returnType = BuiltinType{"I", nil}722 switch t := operandTypes[0].(type) {723 case BuiltinType:724 switch t.Name {725 case "Str":726 code += "_std.StrLen(" + operandCode[0] + ")"727 case "L":728 code += "int64(len(*" + operandCode[0] + "))"729 case "M", "S":730 code += "int64(len(" + operandCode[0] + "))"731 default:732 return "", nil, msg(o.LineNumber, o.Column, "len operand must be a list or map")733 }734 case ArrayType:735 code += "int64(len(" + operandCode[0] + "))"736 default:737 return "", nil, msg(o.LineNumber, o.Column, "len operation requires a list, map, array, or slice as operand")738 }739 case "istype":740 if len(o.Operands) != 2 {741 return "", nil, msg(o.LineNumber, o.Column, "istype operation requires two operands")742 }743 parsedType, ok := o.Operands[0].(ParsedDataType)744 if !ok {745 return "", nil, msg(o.LineNumber, o.Column, "istype first operand must be a data type")746 }747 dt, err := getDataType(parsedType, pkg)748 if err != nil {749 return "", nil, err750 }751 if !isType(dt, operandTypes[1], false) {752 return "", nil, msg(o.LineNumber, o.Column, "istype first operand must be a type implementing "+753 "interface type of the second operand")754 }755 code += operandCode[1] + ".(" + operandCode[0] + "))"756 return code, []DataType{dt, BuiltinType{"Bool", nil}}, nil757 case "randNum":758 if len(o.Operands) > 0 {759 return "", nil, msg(o.LineNumber, o.Column, "randFloat operation takes no operands")760 }761 returnType = BuiltinType{"F", nil}762 code += "_std.RandFloat()"763 case "floor":764 if len(o.Operands) != 1 {765 return "", nil, msg(o.LineNumber, o.Column, "'floor' operation takes one float operand")766 }767 returnType = BuiltinType{"F", nil}768 if !isType(operandTypes[0], BuiltinType{"F", nil}, true) {769 return "", nil, msg(o.LineNumber, o.Column, "'floor' operation has non-float operand")770 }771 code += "_std.Floor(" + operandCode[0] + ")"772 case "ceil":773 if len(o.Operands) != 1 {774 return "", nil, msg(o.LineNumber, o.Column, "'ceil' operation takes one float operand")775 }776 returnType = BuiltinType{"F", nil}777 if !isType(operandTypes[0], BuiltinType{"F", nil}, true) {778 return "", nil, msg(o.LineNumber, o.Column, "'ceil' operation has non-float operand")779 }780 code += "_std.Ceil(" + operandCode[0] + ")"781 case "parseInt":782 if len(o.Operands) != 1 {783 return "", nil, msg(o.LineNumber, o.Column, "parseInt operation takes one string operand")784 }785 if !isType(operandTypes[0], BuiltinType{"Str", nil}, true) {786 return "", nil, msg(o.LineNumber, o.Column, "parseInt operation has non-string operand")787 }788 code += "_std.ParseInt(" + operandCode[0] + "))"789 return code, []DataType{BuiltinType{"I", nil}, BuiltinType{"Str", nil}}, nil790 case "parseFloat":791 if len(o.Operands) != 1 {792 return "", nil, msg(o.LineNumber, o.Column, "parseFloat operation takes one string operand")793 }794 returnType = BuiltinType{"F", nil}795 if !isType(operandTypes[0], BuiltinType{"Str", nil}, true) {796 return "", nil, msg(o.LineNumber, o.Column, "parseFloat operation has non-string operand")797 }798 code += "_std.ParseFloat(" + operandCode[0] + "))"799 return code, []DataType{BuiltinType{"F", nil}, BuiltinType{"Str", nil}}, nil800 case "formatInt":801 if len(o.Operands) != 1 {802 return "", nil, msg(o.LineNumber, o.Column, "formatInt operation takes one integer operand")803 }804 returnType = BuiltinType{"Str", nil}805 if !isType(operandTypes[0], BuiltinType{"I", nil}, true) {806 return "", nil, msg(o.LineNumber, o.Column, "formatInt operation has non-integer operand")807 }808 code += "_std.FormatInt(" + operandCode[0] + ")"809 case "formatFloat":810 if len(o.Operands) != 1 {811 return "", nil, msg(o.LineNumber, o.Column, "formatFloat operation takes one float operand")812 }813 returnType = BuiltinType{"Str", nil}814 if !isType(operandTypes[0], BuiltinType{"F", nil}, true) {815 return "", nil, msg(o.LineNumber, o.Column, "formatFloat operation has non-float operand")816 }817 code += "_std.FormatFloat(" + operandCode[0] + ")"818 case "parseTime":819 if len(o.Operands) != 1 {820 return "", nil, msg(o.LineNumber, o.Column, "parseTime operation takes one string operand")821 }822 if !isType(operandTypes[0], BuiltinType{"Str", nil}, true) {823 return "", nil, msg(o.LineNumber, o.Column, "parseTime operation has non-string operand")824 }825 code += "_std.ParseTime(" + operandCode[0] + ")"826 return code, []DataType{BuiltinType{"I", nil}, BuiltinType{"Err", nil}}, nil827 case "timeNow":828 if len(o.Operands) != 0 {829 return "", nil, msg(o.LineNumber, o.Column, "TimeNow operation takes no operands")830 }831 returnType = BuiltinType{"I", nil}832 code += "_std.TimeNow()"833 case "formatTime":834 if len(o.Operands) != 1 {835 return "", nil, msg(o.LineNumber, o.Column, "formatTime operation takes one integer operand")836 }837 returnType = BuiltinType{"Str", nil}838 if !isType(operandTypes[0], BuiltinType{"I", nil}, true) {839 return "", nil, msg(o.LineNumber, o.Column, "formatTime operation has non-integer operand")840 }841 code += "_std.FormatTime(" + operandCode[0] + ")"842 case "createFile":843 if len(o.Operands) != 1 {844 return "", nil, msg(o.LineNumber, o.Column, "'createFile' operation takes one string operand")845 }846 if !isType(operandTypes[0], BuiltinType{"Str", nil}, true) {847 return "", nil, msg(o.LineNumber, o.Column, "'cerateFile' operation has non-string operand")848 }849 code += "_std.CreateFile(" + operandCode[0] + ")"850 code += ")"851 return code, []DataType{BuiltinType{"I", nil}, BuiltinType{"Str", nil}}, nil852 case "openFile":853 if len(o.Operands) != 1 {854 return "", nil, msg(o.LineNumber, o.Column, "'openFile' operation takes one string operand")855 }856 if !isType(operandTypes[0], BuiltinType{"Str", nil}, true) {857 return "", nil, msg(o.LineNumber, o.Column, "'openFile' operation has non-string operand")858 }859 code += "_std.OpenFile(" + operandCode[0] + ")"860 code += ")"861 return code, []DataType{BuiltinType{"I", nil}, BuiltinType{"Str", nil}}, nil862 case "closeFile":863 if len(o.Operands) != 1 {864 return "", nil, msg(o.LineNumber, o.Column, "'closeFile' operation takes one integer operand")865 }866 if !isType(operandTypes[0], BuiltinType{"I", nil}, true) {867 return "", nil, msg(o.LineNumber, o.Column, "'closeFile' operation has non-string operand")868 }869 code += "_std.CloseFile(" + operandCode[0] + ")"870 code += ")"871 return code, []DataType{BuiltinType{"Str", nil}}, nil872 case "readFile":873 if len(o.Operands) != 2 {874 return "", nil, msg(o.LineNumber, o.Column, "'readFile' operation takes one integer and one slice of bytes")875 }876 if !isType(operandTypes[0], BuiltinType{"I", nil}, true) {877 return "", nil, msg(o.LineNumber, o.Column, "'readFile' first operator should be an integer (a file id)")878 }879 if !isType(operandTypes[1], BuiltinType{"S", []DataType{BuiltinType{"Byte", nil}}}, true) {880 return "", nil, msg(o.LineNumber, o.Column, "'readFile' second operator should be a slice of bytes")881 }882 code += "_std.ReadFile(" + operandCode[0] + "," + operandCode[1] + ")"883 code += ")"884 return code, []DataType{BuiltinType{"I", nil}, BuiltinType{"Str", nil}}, nil885 case "writeFile":886 if len(o.Operands) != 2 {887 return "", nil, msg(o.LineNumber, o.Column, "'writeFile' operation takes one integer and one slice of bytes")888 }889 if !isType(operandTypes[0], BuiltinType{"I", nil}, true) {890 return "", nil, msg(o.LineNumber, o.Column, "'writeFile' first operator should be an integer (a file id)")891 }892 if !isType(operandTypes[1], BuiltinType{"S", []DataType{BuiltinType{"Byte", nil}}}, true) {893 return "", nil, msg(o.LineNumber, o.Column, "'writeFile' second operator should be a slice of bytes")894 }895 code += "_std.WriteFile(" + operandCode[0] + "," + operandCode[1] + ")"896 code += ")"897 return code, []DataType{BuiltinType{"I", nil}, BuiltinType{"Str", nil}}, nil898 case "seekFile":899 if len(o.Operands) != 2 {900 return "", nil, msg(o.LineNumber, o.Column, "'seekFile' operation takes one integer and one slice of bytes")901 }902 if !isType(operandTypes[0], BuiltinType{"I", nil}, true) {903 return "", nil, msg(o.LineNumber, o.Column, "'seekFile' first operator should be an integer (a file id)")904 }905 if !isType(operandTypes[1], BuiltinType{"S", []DataType{BuiltinType{"Byte", nil}}}, true) {906 return "", nil, msg(o.LineNumber, o.Column, "'seekFile' second operator should be a slice of bytes")907 }908 code += "_std.SeekFile(" + operandCode[0] + "," + operandCode[1] + ")"909 code += ")"910 return code, []DataType{BuiltinType{"I", nil}, BuiltinType{"Str", nil}}, nil911 case "seekFileStart":912 if len(o.Operands) != 2 {913 return "", nil, msg(o.LineNumber, o.Column, "'seekFileStart' operation takes one integer and one slice of bytes")914 }915 if !isType(operandTypes[0], BuiltinType{"I", nil}, true) {916 return "", nil, msg(o.LineNumber, o.Column, "'seekFileStart' first operator should be an integer (a file id)")917 }918 if !isType(operandTypes[1], BuiltinType{"S", []DataType{BuiltinType{"Byte", nil}}}, true) {919 return "", nil, msg(o.LineNumber, o.Column, "'seekFileStart' second operator should be a slice of bytes")920 }921 code += "_std.SeekFileStart(" + operandCode[0] + "," + operandCode[1] + ")"922 code += ")"923 return code, []DataType{BuiltinType{"I", nil}, BuiltinType{"Str", nil}}, nil924 case "seekFileEnd":925 if len(o.Operands) != 2 {926 return "", nil, msg(o.LineNumber, o.Column, "'seekFileStart' operation takes one integer and one slice of bytes")927 }928 if !isType(operandTypes[0], BuiltinType{"I", nil}, true) {929 return "", nil, msg(o.LineNumber, o.Column, "'seekFileStart' first operator should be an integer (a file id)")930 }931 if !isType(operandTypes[1], BuiltinType{"S", []DataType{BuiltinType{"Byte", nil}}}, true) {932 return "", nil, msg(o.LineNumber, o.Column, "'seekFileStart' second operator should be a slice of bytes")933 }934 code += "_std.SeekFileEnd(" + operandCode[0] + "," + operandCode[1] + ")"935 code += ")"936 return code, []DataType{BuiltinType{"I", nil}, BuiltinType{"Str", nil}}, nil937 }938 code += ")"939 if returnType == nil {940 return code, []DataType{}, nil941 }942 return code, []DataType{returnType}, nil943}...
line_test.go
Source:line_test.go
...16 }, 0, 0)17 assert.Nil(t, err)18 assert.Equal(t, []types.Line{19 {20 LineNumber: 0,21 Highlights: []types.Section{{0, 1}, {3, 4}},22 },23 {24 LineNumber: 1,25 Highlights: []types.Section{{1, 2}},26 },27 {28 LineNumber: 2,29 Highlights: []types.Section{{1, 5}},30 },31 {32 LineNumber: 3,33 Highlights: []types.Section{{0, 2}},34 },35 }, docs)36 _, _, err = idxr.GetLinesFromSections(1, []types.Section{37 {0, 100},38 }, 0, 0)39 logger.Error(err)40 assert.NotNil(t, err)41 _, _, err = idxr.GetLinesFromSections(1, []types.Section{42 {10, 1},43 }, 0, 0)44 logger.Error(err)45 assert.NotNil(t, err)46 docs, _, err = idxr.GetLinesFromSections(1, []types.Section{47 {3, 4}, {11, 17},48 }, 1, 0)49 assert.Nil(t, err)50 assert.Equal(t, []types.Line{51 {52 LineNumber: 0,53 Highlights: []types.Section{{3, 4}},54 },55 {56 LineNumber: 1,57 },58 {59 LineNumber: 2,60 Highlights: []types.Section{{1, 5}},61 },62 {63 LineNumber: 3,64 Highlights: []types.Section{{0, 2}},65 },66 {67 LineNumber: 4,68 Highlights: nil,69 },70 }, docs)71 docs, _, err = idxr.GetLinesFromSections(1, []types.Section{72 {3, 4}, {11, 17},73 }, 1, 0)74 assert.Nil(t, err)75 assert.Equal(t, []types.Line{76 {77 LineNumber: 0,78 Highlights: []types.Section{{3, 4}},79 },80 {81 LineNumber: 1,82 },83 {84 LineNumber: 2,85 Highlights: []types.Section{{1, 5}},86 },87 {88 LineNumber: 3,89 Highlights: []types.Section{{0, 2}},90 },91 {92 LineNumber: 4,93 Highlights: nil,94 },95 }, docs)96}...
LineNumber
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4 fmt.Println(runtime.Caller(0))5 fmt.Println(runtime.Caller(1))6 fmt.Println(runtime.Caller(2))7}8import (9func main() {10 fmt.Println("Hello World")11 fmt.Println(runtime.Caller(0))12 fmt.Println(runtime.Caller(1))13 fmt.Println(runtime.Caller(2))14}
LineNumber
Using AI Code Generation
1import(2func main(){3 fmt.Println("Line Number: ",types.LineNumber())4}5import(6func main(){7 fmt.Println("Line Number: ",types.LineNumber())8}9import(10func main(){11 fmt.Println("Line Number: ",types.LineNumber())12}13import(14func main(){15 fmt.Println("Line Number: ",types.LineNumber())16}17import(18func main(){19 fmt.Println("Line Number: ",types.LineNumber())20}21import(22func main(){23 fmt.Println("Line Number: ",types.LineNumber())24}25import(26func main(){27 fmt.Println("Line Number: ",types.LineNumber())28}29import(30func main(){31 fmt.Println("Line Number: ",types.LineNumber())32}33import(34func main(){35 fmt.Println("Line Number: ",types.LineNumber())36}37import(38func main(){39 fmt.Println("Line Number: ",types.LineNumber())40}41import(42func main(){43 fmt.Println("Line Number: ",types.LineNumber())44}
LineNumber
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println(LineNumber())4}5import "fmt"6func main() {7 fmt.Println(LineNumber())8}9import "fmt"10func main() {11 fmt.Println(LineNumber())12}13import "fmt"14func main() {15 fmt.Println(LineNumber())16}17import "fmt"18func main() {19 fmt.Println(LineNumber())20}21import "fmt"22func main() {23 fmt.Println(LineNumber())24}25import "fmt"26func main() {27 fmt.Println(LineNumber())28}29import "fmt"30func main() {31 fmt.Println(LineNumber())32}33import "fmt"34func main() {35 fmt.Println(LineNumber())36}37import "fmt"38func main() {39 fmt.Println(LineNumber())40}41import "fmt"42func main() {43 fmt.Println(LineNumber())44}
LineNumber
Using AI Code Generation
1import "fmt"2func main(){3 fmt.Println("Line number:",LineNumbers())4}5import "runtime"6func LineNumbers() int{7 _,_,line,_:=runtime.Caller(1)8}
LineNumber
Using AI Code Generation
1import (2func main() {3 t := types.NewNamed(4 types.NewTypeName(0, nil, "MyType", nil),5 types.NewStruct(nil, nil),6 ln := t.Obj().Pos()7 fmt.Println(ln)8}9import (10func main() {11 t := types.NewNamed(12 types.NewTypeName(0, nil, "MyType", nil),13 types.NewStruct(nil, nil),14 ln := t.Obj().Pos()15 fmt.Println(ln.Line())16}17import (18func main() {19 t := types.NewNamed(20 types.NewTypeName(0, nil, "MyType", nil),21 types.NewStruct(nil, nil),22 ln := t.Obj().Pos()23 fmt.Println(ln.Column())24}25import (26func main() {27 t := types.NewNamed(28 types.NewTypeName(0, nil, "MyType", nil
LineNumber
Using AI Code Generation
1import (2func main() {3 fmt.Println("Line number: ", LineNumber())4}5func LineNumber() int {6 return runtime.FuncForPC(reflect.ValueOf(LineNumber).Pointer()).Line()7}8import (9func main() {10 fmt.Println("File name: ", FileName())11 fmt.Println("Line number: ", LineNumber())12 fmt.Println("File name and line number: ", FileLine())13}14func FileName() string {15 return runtime.FuncForPC(reflect.ValueOf(FileName).Pointer()).Name()16}17func LineNumber() int {18 return runtime.FuncForPC(reflect.ValueOf(LineNumber).Pointer()).Line()19}20func FileLine() string {21 return runtime.FuncForPC(reflect.ValueOf(FileLine).Pointer()).FileLine(0)22}23import (24func main() {25 fmt.Println("File name: ", FileName())26 fmt.Println("Line number: ", LineNumber())27 fmt.Println("File name and line number: ", FileLine())28}29func FileName() string {30 _, file, _, _ := runtime.Caller(0)31}32func LineNumber() int {33 _, _, line, _ := runtime.Caller(0)34}35func FileLine() string {36 _, file, line, _ := runtime.Caller(0)37 return file + ":" + fmt.Sprint(line)38}
LineNumber
Using AI Code Generation
1import (2func main() {3 fmt.Println("Line number: ", runtime.Caller(0))4}5import (6func main() {7 fmt.Println("File name: ", runtime.FuncForPC(runtime.Caller(0)).File())8 fmt.Println("Function name: ", runtime.FuncForPC(runtime.Caller(0)).Name())9}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!