-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08bdecb
commit aa965a2
Showing
7 changed files
with
106 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import wso2/nballerina.types as t; | ||
|
||
public function typeWitnessToString(t:WitnessCollector witness) returns string { | ||
t:WitnessValue value = witness.get(); | ||
if value is string { | ||
return value; | ||
} | ||
else if value == () { | ||
return ""; | ||
} | ||
else { | ||
Expr expr = nonStringWitnessToExpr(value); | ||
return exprToString(expr); | ||
} | ||
} | ||
|
||
public function typeWitnessToExpr(t:WitnessValue value) returns Expr { | ||
if value == () || value is string { | ||
return { startPos: 0, endPos: 0, value }; | ||
} | ||
else { | ||
return nonStringWitnessToExpr(value); | ||
} | ||
} | ||
|
||
function nonStringWitnessToExpr(t:WrappedSingleValue|map<t:WitnessValue>|t:ListWitnessValue value) returns Expr { | ||
if value is t:WrappedSingleValue { | ||
return singleValueToLiteral(value); | ||
} | ||
else if value is map<t:WitnessValue> { | ||
return mappingWitnessToLiteral(value); | ||
} | ||
else if value is t:ListWitnessValue { | ||
return listWitnessToLiteral(value); | ||
} | ||
} | ||
|
||
function listWitnessToLiteral(t:ListWitnessValue value) returns ListConstructorExpr { | ||
var { memberValues, indices, fixedLen } = value; | ||
Expr[] members = []; | ||
foreach int i in 0 ..< memberValues.length() { | ||
Expr m = typeWitnessToExpr(memberValues[i]); | ||
if i + 1 < indices.length() { | ||
int nextIndex = indices[i + 1]; | ||
members.push(m); | ||
foreach var _ in members.length() ..< nextIndex { | ||
members.push(m); | ||
} | ||
} | ||
} | ||
|
||
foreach int i in members.length() ..< fixedLen { | ||
members.push(typeWitnessToExpr(memberValues[memberValues.length() - 1])); | ||
} | ||
return { startPos: 0, endPos: 0, opPos: 0, members }; | ||
} | ||
|
||
function mappingWitnessToLiteral(map<t:WitnessValue> value) returns MappingConstructorExpr { | ||
int startPos = 0; | ||
int endPos = 0; | ||
Field[] fields = from var [name, val] in value.entries() | ||
select { startPos, endPos, colonPos: startPos, isIdentifier: true, name, value: typeWitnessToExpr(val) }; | ||
return { startPos, endPos, opPos: startPos, fields }; | ||
} | ||
|
||
function singleValueToLiteral(t:WrappedSingleValue value) returns LiteralExpr|NumericLiteralExpr { | ||
t:SingleValue v = value.value; | ||
int startPos = 0; | ||
int endPos = 0; | ||
if v == () || v is string || v is boolean { | ||
return { startPos, endPos, value: v }; | ||
} | ||
else if v is int { | ||
return { startPos, endPos, base: 10, digits: v.toString() }; | ||
} | ||
else if v is float { | ||
return { startPos, endPos, typeSuffix: "f", untypedLiteral: v.toString() }; | ||
} | ||
else if v is decimal { | ||
return { startPos, endPos, typeSuffix: "d", untypedLiteral: v.toString() }; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters